2 clcurie 11 clcurie_11 于 2013.04.19 17:46 提问

ArrayAdapter 要求ID 是一个 TextView 错误

我想给 list items 创建一个布局,我把代码简化成下面这样才能运行:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

当我再添加一些代码后,它能编辑和运行,但是在开启时被迫关闭,并且给出错误:"ArrayAdapter requires ID to be a TextView."

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon1"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Some more information" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon1"
        android:gravity="center_vertical"
        android:text="Some Information" />

    <ImageView
        android:id="@+id/icon2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>


public class FirstLoginActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] testcontacts = getResources().getStringArray(
                R.array.testcontacts_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
                testcontacts));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }

我发现有效的方法是创建一个静态的ViewHolder类。在网上查找的一个方法是直接访问数据,我也使用这个方法,但还是疑惑该怎么做。

 public class FirstLoginActivity extends ListActivity {
    Context mContext;
    List mList;

    String[] testcontacts = getResources().getStringArray(
            R.array.testcontacts_array);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
                testcontacts));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }



    public View getView(int position, View convertview, ViewGroup parent) {
        ViewHolder holder;
        View v = convertview;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) LayoutInflater
                    .from(mContext);
            v = inflater.inflate(R.layout.list_items, null);
            holder = new ViewHolder();
            holder.firstLine = (TextView) v.findViewById(R.id.firstLine);
            holder.secondLine = (TextView) v.findViewById(R.id.secondLine);
            holder.icon1 = (ImageView) v.findViewById(R.id.icon1);
            holder.icon2 = (ImageView) v.findViewById(R.id.icon2);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.firstLine.setText(testcontacts[position]);
        holder.secondLine.setText(testcontacts[position]);
        // holder.icon1.setImageBitmap((position & 1) == 1 ? mIcon1: mIcon2);
        //call the images directly?
        return v;
    }

    static class ViewHolder {
        TextView firstLine;
        TextView secondLine;
        ImageView icon1;
        ImageView icon2;

    }
}

2个回答

franzhong
franzhong   2013.04.19 19:09

布局文件内根节点必需是TextView好像

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

把它单独提取出来成一个xml文件

oSanYeCao1234567
oSanYeCao1234567   Ds   Rxr 2016.07.28 10:07

ArrayAdapter默认布局就是一个TextView

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!