2012-02-14 119 views
135

我試圖設置我的視圖以顯示要顯示的文件(文本文件)的ListView時出現錯誤。我很確定它與xml有關。我只想顯示來自this.file = fileop.ReadFileAsList("Installed_packages.txt");的信息。我的代碼:「ArrayAdapter需要資源ID爲TextView」xml問題

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> 

回答

354

ArrayAdapter需要的資源ID是一個TextView XML例外意味着你不提供什麼ArrayAdapter預期。當您使用此構造函數:

new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file) 

R.Layout.a_layout_file必須是包含XML佈局文件的ID只有TextView(!在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,也必須包含TextView與和id(第三個參數),你傳遞給你的ArrayAdapter所以它可以知道放在哪裏行佈局中的Strings

+1

有同樣的問題,因爲我的TextView是一個的LinearLayout內的XML文件。 – 2012-11-18 10:55:11

+0

嘗試此自動完成下拉列表中,但它不允許滾動,也沒有允許我從列表中選擇任何項目 – kabuto178 2013-01-31 20:01:13

+17

TextView **可以**包裝在其他佈局(我只是做了它)。要做到這一點使用其他構造函數'新的ArrayAdapter (這,R.layout.a_layout_file,R.id.a_text_view_within_layout,this.file)'看到'android.widget.ArrayAdapter'的javadoc – 2013-04-18 02:34:39

23

Soution在這裏

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); 
+0

我知道這似乎應該是一個沒有理由,但查看ArrayAdapter構造函數的文檔也幫助了我。 [鏈接](http://developer.android.com/reference/android/widget/ArrayAdapter.html) – luckyging3r 2016-03-20 04:59:04

相關問題