您必须为TextView提供资源ID

时间:2012-09-15 12:03:29

标签: android android-listview android-arrayadapter

我搜索了与此问题相关的现有教程,我按照这个例子。但我仍然得到同样的错误。当我更改android.R.layout.simple_list_item1行时,它将无效。

mycode的:

public class Lisearch extends Activity {
    private ListView lv;
    private EditText et;
    private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
    "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
    private ArrayList<String> array_sort= new ArrayList<String>();
    int textlength=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lisearch);
        //TextView tv=(TextView)findViewById(R.id.text);
        lv = (ListView) findViewById(R.id.ListView01);
        et = (EditText) findViewById(R.id.EditText01);
        //lv.setAdapter(new ArrayAdapter<String>(this,
        //R.layout.activity_lisearch, listview_array));

        et.addTextChangedListener(new TextWatcher()
        {
        public void afterTextChanged(Editable s)
        {
                                                                        // Abstract Method of TextWatcher Interface.
        }
        public void beforeTextChanged(CharSequence s,
        int start, int count, int after)
        {
        // Abstract Method of TextWatcher Interface.
        }
        public void onTextChanged(CharSequence s,
        int start, int before, int count)
        {
        textlength = et.getText().length();
        array_sort.clear();
        for (int i = 0; i < listview_array.length; i++)
        {
        if (textlength <= listview_array[i].length())
        {
        if(et.getText().toString().equalsIgnoreCase(
        (String)
        listview_array[i].subSequence(0,
        textlength)))
        {
                                                                                                                        array_sort.add(listview_array[i]);
                                                                                                        }
                                                                                        }
                                                                        }
        lv.setAdapter(new ArrayAdapter<String>
        (Lisearch.this,
        R.layout.activity_lisearch,R.id.text, array_sort));
        }
        });

    }

Xml文件:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text"
        android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search">
</EditText>

<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</TextView>

1 个答案:

答案 0 :(得分:10)

试试这个。使用android.R.layout_simple_list_item_1显示默认列表视图。

lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array_sort));

设置自定义布局

lv.setAdapter(new ArrayAdapter<String>(this,R.layout.conversation_item,R.id.sometextview_from_the_layout,array_sort));

这里

R.layout.conversation_item 是自定义布局, R.id.sometextview_from_the_layout 是来自自定义布局的textview id。

conversation_item.xml文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
/>