【问题标题】:Retrieving Original HashMap from a SimpleAdapter/ListView on Item Press在 Item Press 上从 SimpleAdapter/ListView 检索原始 HashMap
【发布时间】:2011-08-30 00:29:15
【问题描述】:

我有一个填充 ListView 的 SimpleAdapter,但是当用户按下一个项目时,我希望能够获得用于填充所单击行的原始 HashMap。不幸的是,我似乎只能得到一个只支持 toString() 的对象。从用户单击的视图中获取字符串是没有用的,因为它不是唯一标识符。

如何取回用户点击的行的数据?

ArrayList<HashMap<String, String>> reformedBundle = new ArrayList<HashMap<String, String>>();
// [...]
mResultsAdapter = new SimpleAdapter(this,
    reformedBundle, R.layout.resultrow,
    new String[] { "ProviderName", "Street", "City" },
    new int[] { R.id.tvField1, R.id.tvField2, R.id.tvField3
});

lvResults.setAdapter(mResultsAdapter);

lvResults.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            // Here's where I want to get the original HashMap<String, String> so I can query other keys in the HashMap
        }
    });

【问题讨论】:

    标签: java android android-listview simpleadapter


    【解决方案1】:

    我存储在一个成员变量中。

    private ArrayList<HashMap<String, String>> reformedBundle;
    
    ...
    
    lvResults.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            HashMap<String, String> map = reformedBundle.get(pos);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多