“ArrayAdapter 要求资源 ID 为 TextView";XML 问题 [英] "ArrayAdapter requires the resource ID to be a TextView" XML problems
问题描述
尝试将视图设置为显示我要显示的文件(文本文件)的 ListView
时出现错误.我很确定它与 XML 有关.我只想显示来自 this.file = fileop.ReadFileAsList("Installed_packages.txt");
的信息.我的代码:
I am getting an error when trying to set my view to display the ListView
for the file I want to display(text file). I am pretty sure it has something to do with the XML. I just want to display the information from this.file = fileop.ReadFileAsList("Installed_packages.txt");
. My code:
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);
}
}
list_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>
推荐答案
ArrayAdapter 要求资源 ID 为 TextView XML 异常意味着您不提供 ArrayAdapter代码> 期望.当你使用这个构造函数时:
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
必须是仅包含 TextView
的 xml 布局文件的 ID(TextView
可以't 被另一个布局包裹,比如 LinearLayout
、RelativeLayout
等!),像这样:
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
/>
如果你想让你的列表行布局有点不同,那么一个简单的 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)
在其中提供可以包含各种视图的布局的 id
,但也必须包含带有 id 的
TextView
(第三个参数),您传递给 ArrayAdapter
以便它知道将 Strings
放在行布局中的位置.
where you supply the id
of a layout that can contain various views, but also must contain a TextView
with and id
(the third parameter) that you pass to your ArrayAdapter
so it can know where to put the Strings
in the row layout.
这篇关于“ArrayAdapter 要求资源 ID 为 TextView";XML 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!