2012-03-10 78 views
1

請原諒我,我是新手:新手。我使用simple_list_item_2來顯示11個項目。這11個項目已使用HashMap,然後SimpleAdapter加載。這在顯示所有內容時工作正常。問題是我無法獲得setOnItemClickListener。該代碼:使用simple_list_item_2並無法弄清楚如何使用setOnItemClickListener

public class TwoLineActivity extends ListActivity 
{ 

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(2); 

@Override 
public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.scrolllist); 

     // enter valid data, these 2 are the same as the remaining 9 
     HashMap<String, String> maplist; 
     maplist = new HashMap<String, String>(); 
     maplist.put("line1", "a11 data"); 
     maplist.put("line2", "asd asd ad 1234569780"); 
     list.add(maplist); 

     maplist = new HashMap<String, String>(); 
     maplist.put("line1", "a12 data"); 
     maplist.put("line2", "asd asd ad 1234569781"); 
     list.add(maplist); 

String[] from = { "line1", "line2" }; 

int[] to = { android.R.id.text1, android.R.id.text2 }; 

SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to); 
setListAdapter(adapter);` 

因此,直到這裏事情是偉大的,我得到我的名單。現在,我希望能夠從列表中選擇一個項目,所以我編寫了未來2線

list.setAdapter(adapter); 
    list.setOnItemClickListener(new OnItemClickListener() 
    { ..... 

我收到以下錯誤

The method setAdapter(SimpleAdapter) is undefined for the type ArrayList<HashMap<String,String>>

The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type ArrayList<HashMap<String,String>>

+0

*「沒有錯誤。」* - 您會得到什麼特定錯誤?你的應用崩潰了嗎?在這種情況下,請發佈堆棧跟蹤。謝謝! – 2012-03-10 15:46:04

+0

您的活動是否擴展ListActivity或活動? – 2012-03-10 19:17:05

回答

0

如果您的活動擴展了ListActivity,則應該覆蓋

protected void onListItemClick(ListView l, View v, int position, long id) { 

}

+0

你好,抱歉我不好。 – 2012-03-11 00:33:10

+0

謝謝@PascalPiché,但在setAdapter和setOnItemClickListener中存在這個問題(因爲我沒有解釋這個問題)。我重提了這個問題,希望更具體 – 2012-03-11 01:21:04