【问题标题】:Listview to SimpleAdapter not workingListview 到 SimpleAdapter 不起作用
【发布时间】:2014-06-05 08:18:49
【问题描述】:

我在一个 Activity 中有代码,如下所示,它运行良好。

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, catid);
map.put(TAG_NAME, catname);                 
oslist.add(map);
list=(ListView)findViewById(R.id.listView1);                                
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.category_list,
new String[] { TAG_ID,TAG_NAME}, new int[] {
R.id.catid,R.id.catname});

虽然另一个 Activity 中的类似代码不起作用...

ListAdapter adapter = new SimpleAdapter(newsList.this, oslist,
R.layout.category_list,
new String[] { TAG_ID,TAG_NAME}, new int[] {
R.id.catid,R.id.catname});
list.setAdapter(adapter);

它给出如下错误:

The constructor SimpleAdapter(newsList, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined

【问题讨论】:

标签: android listview simpleadapter


【解决方案1】:

如果你有newsList.this,你应该传递一个上下文,可能是Activity2.this,假设你的第二个Activity被称为Activity2。

【讨论】:

    【解决方案2】:

    您必须在适配器的构造函数中传递上下文,例如:

    //声明全局

    Contect _ctx = youractivityname.this
    

    然后将其传递给您的适配器,例如:

    ListAdapter adapter = new SimpleAdapter(_ctx, oslist,R.layout.category_list,new String[] {    TAG_ID,TAG_NAME}, new int[] {R.id.catid,R.id.catname});
    

    【讨论】:

    • 当我写 Context _ctx = newsList.this;它给出了以下错误
    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2023-03-22
    • 2013-07-18
    • 1970-01-01
    相关资源
    最近更新 更多