如何从适配器获取OnItemClickListener中的值?

时间:2014-05-23 20:05:03

标签: android android-adapter onitemclicklistener

请帮助从OnItemClickListener

获取“KEY_ID”的值

适配器代码:

public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}

活动代码:

ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

map.put(MainActivity.KEY_ID,MainActivity.parser.getValue(e,MainActivity.KEY_ID));
songsList.add(map);

adapter = new LazyAdapter(getActivity(), songsList);
list.setAdapter(adapter);

st.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

String sss = ???????????????????????????????????? value of KEY_ID
}
});

请帮助。 感谢您提供的任何帮助

2 个答案:

答案 0 :(得分:1)

尝试将onItemClick更改为:

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

        String sss = songsList.get(position).get(MainActivity.KEY_ID);
    }

答案 1 :(得分:1)

您可以使用适配器,它更可靠。因为它对应于UI当前显示的内容:

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
       int position, long id) {
       String value = ((Map<String, String>)adapter.getItem(position)).get(MainActivity.KEY_ID);

     // 'adapter' can be either the field variable of the one you instantiated above OR by accessing the parameter 'parent' like this: parent.getAdapter()

    }