首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >"ArrayAdapter要求资源ID必须是TextView“XML问题

"ArrayAdapter要求资源ID必须是TextView“XML问题
EN

Stack Overflow用户
提问于 2012-02-15 00:59:55
回答 3查看 157.2K关注 0票数 192

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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-15 02:16:35

ArrayAdapter要求资源ID为TextView XML异常,这意味着您没有提供ArrayAdapter期望的内容。使用此构造函数时:

代码语言:javascript
复制
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)

R.Layout.a_layout_file必须是只包含TextView的xml布局文件的id ( TextView 不能被另一个布局包装,如LinearLayoutRelativeLayout等!),如下所示:

代码语言:javascript
复制
<?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小部件就可以使用这个构造函数:

代码语言:javascript
复制
new ArrayAdapter<String>(this, R.layout.a_layout_file, 
   R.id.the_id_of_a_textview_from_the_layout, this.file)

其中,您提供了可以包含各种视图的布局的id,但还必须包含您传递给ArrayAdapterTextViewid(第三个参数),这样它就可以知道将Strings放在行布局中的什么位置。

票数 459
EN

Stack Overflow用户

发布于 2015-01-27 00:34:14

这里有Soution

listitem.xml

代码语言:javascript
复制
<?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代码:

代码语言:javascript
复制
 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);
票数 32
EN

Stack Overflow用户

发布于 2018-04-12 03:19:00

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

代码语言:javascript
复制
    //Pass in the resource id:  R.id.text_view
    SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
            R.id.text_view,
            new ArrayList<>());

适配器:

代码语言:javascript
复制
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;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9280965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档