使用simple_list_item_2并且无法弄清楚如何使用setOnItemClickListener

时间:2012-03-10 15:41:23

标签: android menu onclick simpleadapter

请原谅,我是新人:新人。我使用simple_list_item_2来显示11个项目。使用HashMap然后SimpleAdapter加载了这11个项目。这可以很好地显示一切。问题是我不能让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);`

所以在这里事情很棒,我得到了我的清单。现在我希望能够从列表中选择一个项目,所以我编写了接下来的两行

    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>>

1 个答案:

答案 0 :(得分:0)

如果您的活动扩展了ListActivity,则应覆盖

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

}