问题描述
- 安卓,baseadapter,报错,求指教
-
logcat如下:
开发-安卓,baseadapter,报错,求指教-安卓baseadapter">content_main:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview"/> </LinearLayout>
listview的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview2"/> </LinearLayout>
baseadapter的代码:
public class MyAdapter extends BaseAdapter { Context context; String[] tx1; String[] tx2; MyAdapter(Context context,String[] tx1,String[] tx2) { this.context=context; this.tx1=tx1; this.tx2=tx2; } @Override public int getCount() { return tx1.length; } @Override public Object getItem(int position) { return tx1[position]; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position,View v,ViewGroup viewGroup) { View view; ViewHolder viewHolder; if(v == null) { view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup); ; TextView t1 = (TextView) view.findViewById(R.id.textview1); TextView t2 = (TextView) view.findViewById((R.id.textview2)); t1.setText(tx1[position]); t2.setText(tx2[position]); viewHolder=new ViewHolder(t1,t2); view.setTag(viewHolder); } else { view=v; viewHolder=(ViewHolder)view.getTag(); } return v; } class ViewHolder { public TextView t1; public TextView t2; ViewHolder(TextView t1,TextView t2) { this.t1=t1; this.t2=t2; } } }
mainactivity中与listview有关的也就是:
ListView listView; String[] str1={"这是1","这是2","这是3","这是4","这是5"}; String[] str2={"this is 1","this is 2","this is 3","this is 4","this is 5"}; listView=(ListView)super.findViewById(R.id.listview); MyAdapter myAdapter=new MyAdapter(this,str1,str2); listView.setAdapter(myAdapter);
求指教,怎么解决?
解决方案
settext放到return上面去,
解决方案二:
if(v == null) {
view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup);
改成
if(v == null) {
view = LayoutInflater.from(context).inflate(R.layout.listview, null);//传入viewGroup,则布局方式的layout不对
解决方案三:
view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup); viewGroup是默认把父布局ListView传进去,
你inflate生成的View会被通过addView的方式add到父布局也就是ListView里面,listView是不可以addView的,
listView是AdapterView的子类,所以会报错
改成 view = LayoutInflater.from(context).inflate(R.layout.listview, null);
或者 view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup,false);都可以
解决方案四:
view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup);
;
viewGroup 改为 null 就好了
时间: 2024-11-03 16:53:50