2011-11-13 91 views
0

我正在編寫新聞應用程序,並且在顯示自定義列表時遇到了一些問題。所有我想要的是列表中的項目有2個TextViews在其中:自定義陣列適配器

  • 新聞標題和
  • 新聞說明

這些包含在2個靜態數組:homeScreen.title []和主屏幕.descriptionLong []。

下你有我的HashMap和適配器代碼:

最終靜態的ArrayList>數據=新的ArrayList>();

static{ 
    HashMap<String, String> row = new HashMap<String, String>(); 
    for (int i = 0; i<HomeScreen.arrayLength; i++){ 
     row.put("Title", HomeScreen.title[i]); 
     row.put("Description", HomeScreen.descriptionLong[i]); 
     data.add(row); 
    } 
} 

    SimpleAdapter adapter = new SimpleAdapter(this, 
      data, 
       R.layout.mainmenu, 
       new String[] {"Title", "Description"}, 
       new int[] { R.id.textView1, R.id.textView2}); 
    setListAdapter(adapter); 
} 

    public void onItemClick(SimpleAdapter arg0, View arg1, int position, 
       long id) { 
      selectedNews = position; 
      Toast.makeText(getApplicationContext(), "This is: " + selectedNews, Toast.LENGTH_LONG).show(); 
      Intent intent = new Intent(MainMenu.this, ReadNews.class); 
      startActivity(intent); 
     } 

我遇到的問題是,它只顯示我的第20(最後)的新聞信息,也默認的OnItemClick不再工作。我會感激你的幫助......

回答

2

你應該把row實例化for循環中:

for (int i = 0; i<HomeScreen.arrayLength; i++){ 
    HashMap<String, String> row = new HashMap<String, String>(); 
    row.put("Title", HomeScreen.title[i]); 
    row.put("Description", HomeScreen.descriptionLong[i]); 
    data.add(row); 
} 

更新:

OnItemClickListener登記的,應當是這個樣子:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
     // your code here 
    } 
}); 
+0

非常感謝。這是一個很大的幫助。我仍然有問題。默認的OnItemClick不起作用。我將編輯並添加上面的代碼。 – Eugen

+0

因爲花時間和幫助像我這樣的新手而感謝你! – Eugen