2012-03-10 60 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