我想设置我的视图来显示的ListView
因为我想显示(文本文件)的文件时,得到一个错误。我是pretty的确保它有什么做的XML。我只是想显示从 this.file = fileop.ReadFileAsList(Installed_packages.txt)中的信息;
。我的code:
公共类主要扩展活动{
私人TextView的电视;
私人FileOperations fileop;
私有String []文件;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
this.fileop =新FileOperations();
this.file = fileop.ReadFileAsList(Installed_packages.txt);
的setContentView(R.layout.main);
电视=(TextView中)findViewById(R.id.TextView01);
ListView的LV =新的ListView(本);
lv.setTextFilterEnabled(真正的);
lv.setAdapter(新ArrayAdapter<字符串>(这一点,R.layout.list_item,this.file));
lv.setOnItemClickListener(新AdapterView.OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
//单击时,显示出与TextView的文字敬酒
Toast.makeText(getApplicationContext(),((TextView的)视图).getText(),Toast.LENGTH_SHORT).show();
}
});
的setContentView(LV);
}
}
list_item.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:填充=10dp
机器人:TEXTSIZE =16SP
机器人:文字颜色=#000>
< / LinearLayout中>
的main.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:weightSum =1>
<滚动型
机器人:ID =@ + ID / SCROLLER_ID
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:滚动条=垂直
机器人:fillViewport =真正的>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:填充=5SP
机器人:ID =@ + ID / TextView01
机器人:文本=@字符串/你好/>
< /滚动型>
< / LinearLayout中>
解决方案
在 ArrayAdapter需要的资源ID是一个TextView XML 的异常意味着你不提供什么 ArrayAdapter
预期。当您使用此构造函数:
新ArrayAdapter<字符串>(这一点,R.layout.a_layout_file,this.file)
R.Layout.a_layout_file
必须包含一个XML布局文件的ID只有的TextView
(即的TextView
不能按另一个布局包裹,就像一个的LinearLayout
, RelativeLayout的
等),这样的事情:
< XML版本=1.0编码=UTF-8&GT?;
< TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
//其他TextView的属性
/>
如果你想让你的列表行布局是东西有点不同,那么一个简单的的TextView
小工具使用此构造函数:
新ArrayAdapter<字符串>(这一点,R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout,this.file)
在您提供的 ID
,可以包含各种意见的布局,也必须包含的TextView
与和 ID
(第三个参数),你传递给你的 ArrayAdapter
所以它可以知道放在哪里字符串
的行布局。
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>
解决方案
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)
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.