【问题标题】:Using simple_list_item_2 and can't figure out how to use setOnItemClickListener使用 simple_list_item_2 并且无法弄清楚如何使用 setOnItemClickListener
【发布时间】:2012-03-10 15:41:23
【问题描述】:

请原谅,我是新人:这方面的新手。我使用 simple_list_item_2 来显示 11 个项目。这 11 项已通过使用 HashMapSimpleAdapter 加载。这在显示所有内容时效果很好。问题是我不能让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&lt;HashMap&lt;String,String&gt;&gt;

The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type ArrayList&lt;HashMap&lt;String,String&gt;&gt;

【问题讨论】:

  • “没有错误。” - 你得到什么具体错误?你的应用程序崩溃了吗?在这种情况下,请发布堆栈跟踪。谢谢!
  • 您的活动扩展了 ListActivity 或活动?

标签: android menu onclick simpleadapter


【解决方案1】:

如果你的活动扩展了 ListActivity,你应该重写

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

}

【讨论】:

  • 感谢@Pascal Piché,但 setAdapter 和 setOnItemClickListener 中的问题(因为我也没有解释清楚)。我重新提出了这个问题,希望更具体
猜你喜欢
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
相关资源
最近更新 更多