ArrayAdapter要求ID为TextView错误

时间:2011-12-29 21:44:10

标签: android listview textview android-arrayadapter relativelayout

我正在尝试为列表项创建一个不错的布局,但我的代码只有在这样简化时才有效:

<?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;

    }
}

4 个答案:

答案 0 :(得分:2)

你可能正在使用这样的东西(here the doc):

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.layout_1, values);

在这种情况下,您的布局必须是带有TextView的简单布局。

如果您想使用自己的布局,则需要编写自定义适配器。

答案 1 :(得分:1)

构建您自己的ArrayAdapter,然后您可以根据需要使布局工作。

答案 2 :(得分:0)

ArrayAdapter要求资源ID为TextView XML异常,这意味着您不提供ArrayAdapter所期望的内容。当您使用此构造函数时:

new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)

R.Layout.a_layout_file必须是仅包含TextView的xml布局文件的id(TextView不能被其他布局包装,如LinearLayout,RelativeLayout等!),如下所示:

<?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="wrap_content" 
    // other attributes of the TextView
/>

如果您希望列表行布局略有不同,那么简单的TextView小部件就会使用此构造函数:

new ArrayAdapter<String>(this, R.layout.a_layout_file, 
   R.id.the_id_of_a_textview_from_the_layout, this.file)

您提供可包含各种视图的布局的ID,但也必须包含传递给ArrayAdapter的TextView with和id(第三个参数),以便它可以知道在行布局中将字符串放在何处

答案 3 :(得分:0)

你似乎是在正确的路线上。我不确定您的代码的确切问题,因为我没有仔细比较,但它适用于this example

完整的教程在这里:Android tutorial for beginners - 97 - ListView with Custom Layout

这可以避免我遇到的错误ArrayAdapter requires ID to be a TextView