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

尝试将视图设置为显示要显示的文件(文本文件)的列表视图时出错。我很确定这与XML有关。我只想显示this.file=fileop.ReadFileAsList(“Installed”u packages.txt)中的信息。我的代码:

公共类主扩展活动{
私家图文电视;
私人文件操作文件;
私有字符串[]文件;
/**首次创建活动时调用。*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.fileop=新文件操作();
this.file=fileop.ReadFileAsList(“Installed”u packages.txt“);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.TextView01);
ListView lv=新的ListView(本);
lv.setTextFilterEnabled(真);
lv.setAdapter(新的ArrayAdapter<String>(this,R.layout.list_项,this.file));
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?>父级、视图、整型位置、长id){
//单击后,显示带有文本视图文本的祝酒词
Toast.makeText(getApplicationContext(),((TextView)视图).getText(),Toast.LENGTH_SHORT.show();
} 
});         
setContentView(lv);
}
}

列表项.xml

<?xml版本="1.0“;编码="utf-8“&燃气轮机;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="填写“家长”;
android:layout_height="填写“家长”;
android:orientation="垂直的;
android:padding="10dp“;
android:textSize="16sp"
android:textColor=&quot#000“&燃气轮机;
&lt/线性布局>

main.xml

<?xml版本="1.0“;编码="utf-8“&燃气轮机;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="垂直的;
android:layout_width="填写“家长”;
android:layout_height="填写“家长”;
android:weightSum="1“&燃气轮机;
<卷轴视图
android:id=&[email protected]+id/滚动条“U id”;
android:layout_width="填写“家长”;
android:layout_height="包装“U内容”;
android:scrollbars="垂直的;
android:fillViewport="“真的”&燃气轮机;
<文本视图
android:layout_width="填写“家长”;
android:layout_height="包装“U内容”;
android:padding="5sp“;
android:id=&[email protected]+id/TextView01"
android:text=&[email protected]/hello&quot/&燃气轮机;
&lt/滚动视图>
&lt/线性布局>

ArrayAdapter要求资源ID为TextView XML异常意味着您无法提供ArrayAdapter所需的内容。使用此构造函数时:

新阵列适配器<字符串>(this,R.layout.a_layout_文件,this.file)

R.Layout.a_Layout_文件必须是只包含TextView的xml布局文件的id(不能用另一个布局来包装TextView,例如LinearLayoutRelativeLayout等!),类似这样:

<?xml version=“1.0”encoding=“utf-8”?>
<TextView xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout\u width=“fill\u parent”
android:layout\u height=“包装内容”
//TextView的其他属性
/&燃气轮机;

如果您希望列表行布局有点不同,那么一个简单的TextView小部件可以使用以下构造函数:

新阵列适配器<字符串>(这是R.layout.a_layout_文件,
R.id.a\u文本视图的\u id\u来自\u布局,this.file)

如果您提供可以包含各种视图的布局的id,但必须包含TextView,以及传递给ArrayAdapterid(第三个参数),以便它可以知道在行布局中放置字符串的位置

发表评论