“ ArrayAdapter要求资源ID为TextView ” XML问题

尝试将视图设置为显示要显示的文件(文本文件)的ListView时,出现错误。我很确定这与XML有关。我只想显示this.file = fileop.ReadFileAsList("Installed_packages.txt");中的信息。我的代码:

public class Main extends Activity {
private TextView tv;
private FileOperations fileop;
private String[] file;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fileop = new FileOperations();
this.file = fileop.ReadFileAsList("Installed_packages.txt");
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
ListView lv = new ListView(this);
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
lv.setOnItemClickListener(new AdapterView.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();
}
});
setContentView(lv);
}
    

}

列出_item.XML

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


</LinearLayout>

main.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5sp"
android:id="@+id/TextView01"
android:text="@string/hello"/>
</ScrollView>
    

</LinearLayout>
165997 次浏览

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不能由另一个布局包装,如LinearLayoutRelativeLayout等),如下所示:

<?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,而且必须包含您传递给ArrayAdapterTextViewid(第三个参数),以便它可以知道在行布局中放置Strings的位置。

索蒂翁来了

ListItem.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>

Java代码:

 String[] countryArray = {"India", "Pakistan", "USA", "UK"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

如果您在扩展ArrayAdapter时收到该消息,则会收到该错误,因为您没有提供正确的资源ID来显示该项目。在构造函数中调用超类,并传入TextView的资源ID:

    //Pass in the resource id:  R.id.text_view
SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
R.id.text_view,
new ArrayList<>());

适配器:

public class SpinnerAdapter extends ArrayAdapter<MyEntity> {


private Context context;
private List<MyEntity> values;


public SpinnerAdapter(Context context, int textViewResourceId,
List<MyEntity> values) {


//Pass in the resource id:  R.id.text_view
super(context, textViewResourceId, values);


this.context = context;
this.values = values;
}

问题出在这条线上:

 lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));

因为当我把它改成:

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

这很有效!