TextView和EditText的ListView

时间:2014-05-22 13:04:55

标签: android android-layout android-intent android-fragments android-listview

我为列表的每个元素都有这个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/empty_id"
 >

<TextView
    android:id="@+id/textView_frag_empty_codice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label_frag_empty_codice"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText_frag_empty_codice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="74dp"
    android:layout_toRightOf="@+id/textView_frag_empty_codice"
    android:ems="10" >

    <requestFocus />
</EditText>
....

如何创建适配器? 现在我收到一个错误:“错误:ArrayAdapter需要资源ID为TextView” 所以我需要捕获这个listview的输入字段,怎么做呢?

3 个答案:

答案 0 :(得分:2)

ArrayAdapter要求资源ID为TextView XML异常,这意味着您无法提供ArrayAdapter所期望的内容。当您使用此构造函数时:     new ArrayAdapter(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(第三个参数),以便它可以知道在行布局中将字符串放在何处

答案 1 :(得分:0)

简单!您不应该将ArrayAdapter与组合混合使用,ArrayAdapter布局应该只包含TextView Widget。如果你想使用其他TextView,那么你应该去BaseAdapter。

BaseAdapter可以混合使用小部件..

答案 2 :(得分:0)

The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:

new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:

<?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
/>
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:

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


NOTE:In your case you have to use custom adapter.and i think you are using simple adapter.

Thanks