AnnBnnCnnD 2013-03-21 02:39 采纳率: 0%
浏览 5528

从listView中获取单独的值

有一个listView,当点击的时候会显示如下内容:

{first_name=abc, last_name=xyz, id=1, address=kolkata}

但是我希望获取的时候可以根据字符串单独获取。

代码:

onListItemClick监听器

protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    String return_data = o.toString();

    Toast.makeText(this,  ""+return_data, Toast.LENGTH_LONG).show();

}

添加全部类,包括列表适配器:

public class showUserInfoListActivity extends ListActivity {

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
static final String KEY_ID         = "id";
static final String KEY_FIRST_NAME = "first_name";
static final String KEY_LAST_NAME  = "last_name";
static final String KEY_ADDRESS    = "address";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_user_info_list);

    //---get all Records---
    DataBaseAdapter db = new DataBaseAdapter(this);
    db.open();
    Cursor c = db.getAllRecords();
    if (c.moveToFirst())
    {
        do 
        {          
            HashMap<String, String> map = new HashMap<String, String>();
            // adding each child node to HashMap key => value
            map.put(KEY_ID, c.getString(0));
            map.put(KEY_FIRST_NAME, c.getString(1));
            map.put(KEY_LAST_NAME, " "+c.getString(2));
            map.put(KEY_ADDRESS, c.getString(3));

            // adding HashList to ArrayList
            menuItems.add(map);
        } while (c.moveToNext());
    }
    db.close();

    // Adding menuItems to ListView
    // All filed data are not shown in the list KEY_ID is hidden
    ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.user_info_list_item,
                          new String[] { KEY_FIRST_NAME, KEY_LAST_NAME, KEY_ADDRESS, KEY_ID }, 
                          new int[] {R.id.first_name , R.id.last_name, R.id.address});
    setListAdapter(adapter);
}


//On select from the list show data
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    //String return_data = o.toString();
    MyClass return_data = (MyClass)o;
    Toast.makeText(this,  ""+return_data, Toast.LENGTH_LONG).show();
}

class MyClass{

}
}
  • 写回答

1条回答 默认 最新

  • koful43 2014-03-14 08:27
    关注

    HashMap map = (HashMap)this.getListAdapter().getItem(position);
    String first_name=map.get("first_name");
    String last_name=map.get("last_name");
    String id=map.get("id");
    String address=map.get("address");

    我也遇到了同样的问题,我是这么解决的....

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作