Android 我的自定义列表视图不工作
我想创建一个自定义列表视图,但当我运行该程序时,它会出现异常: logcat:Android 我的自定义列表视图不工作,android,android-listview,Android,Android Listview,我想创建一个自定义列表视图,但当我运行该程序时,它会出现异常: logcat: 在onCreate方法中: ListView list = (ListView) findViewById(android.R.id.list); String items[] = {"cat", "dog", "horse", "monkey"}; list.setAdapter(new myAdapter(items)); 在myAdapter类中: private class my
在onCreate方法中:
ListView list = (ListView) findViewById(android.R.id.list);
String items[] = {"cat", "dog", "horse", "monkey"};
list.setAdapter(new myAdapter(items));
在myAdapter类中:
private class myAdapter extends ArrayAdapter<String>{
private String items[];
public myAdapter(String items[]) {
super(ListViewExample.this, R.layout.row, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.row, parent);
TextView label=(TextView)view.findViewById(R.id.row_textView1);
label.setText(items[position]);
return view;
}
}
私有类myAdapter扩展了ArrayAdapter{
私有字符串项[];
公共myAdapter(字符串项[]){
super(ListViewExample.this、R.layout.row、items);
这个项目=项目;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气机=getLayoutInflater();
视图=充气机。充气(R.layout.row,父级);
TextView标签=(TextView)view.findViewById(R.id.row\u textView1);
label.setText(项目[位置]);
返回视图;
}
}
我的主要活动:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
和我的模板行:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/row_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
我如何解决这个问题?谢谢在对行进行充气时,您忘记将第三个参数(attachToRoot)设置为false 试试这个:
View view = inflater.inflate(R.layout.row, parent, false);
如果可用,最好重新使用convertView:
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.row, parent, false);
}
膨胀行时忘记将第三个参数(attachToRoot)设置为false 试试这个:
View view = inflater.inflate(R.layout.row, parent, false);
如果可用,最好重新使用convertView:
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.row, parent, false);
}
膨胀你的观点,如:
View view = inflater.inflate(R.layout.row, parent,false);
AttachToRoot(第三个参数):膨胀的层次结构是否应附加到根参数?如果为false,则root仅用于为XML中的root视图创建正确的LayoutParams子类
View view = inflater.inflate(R.layout.row, parent,false);
AttachToRoot(第三个参数):膨胀的层次结构是否应附加到根参数?如果为false,则root仅用于为XML中的root视图创建正确的LayoutParams子类
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row, null);
像这样膨胀
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row, null);
使用converView而不是view,并尝试使用converView而不是view和TryTanks获取您的答案谢谢您的回答