2011-10-28 40 views
1

我下面the List View tutorial安卓:與列表視圖教程在屏幕上

什麼,我有這樣的代碼(稍作修改):

public class GroupsActivity extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setListAdapter(new ArrayAdapter<String>(this, R.layout.groups_layout, R.array.repetitions_array)); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new 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(); 
      } 
     }); 
    } 
} 

R.layout.groups_layout是一個.xml :

<?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="fill_parent" 
android:padding="10dp" 
android:textSize="16sp" > 
</TextView> 

問題是我在屏幕上什麼都看不到。 任何想法有什麼不對?

+1

您是否在Logcat輸出中看到任何錯誤?你確定你的數組包含項目嗎?也許嘗試將您的listview項目layout_height設置爲「wrap_content」而不是「fill_parent」 – hooked82

+0

您在哪裏定義'repetitions_array'?你是否真的用數據填充它? –

回答

3

你不能提供一個ArrayAdapter構造一個數組資源的ID。你需要實際的數組對象。試試這個:

String your_array_contents[] = context.getResources().getStringArray(R.array.repetitions_array); 
setListAdapter(new ArrayAdapter<String>(this, R.layout.groups_layout, your_array_contents)); 
0

一種可能性是,R.array.repetitions_array空