Android:如何通过onclick从SimpleAdapter返回选定的数据集?

时间:2012-04-21 07:27:34

标签: android listview

我正在尝试使用SimpleAdapter,但我正在努力解决一个问题。

如何返回所选视图的数据数组?

这是我用来生成它的代码。

// Data for List Adapter
List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

// Make Hashmap of Results Hashmaps
generateData(resourceNames);

//Source information from Hashmap keys (FROM)
String keynames[]={"names","descriptions"};

// Target Views for Data (TO) 
int[] targets = new int[] { R.id.textfield_name,R.id.textfield_description};

// Create my Simple Adapter
SimpleAdapter adapter = new SimpleAdapter(this, resourceNames,
        R.layout.listrow, keynames,targets
        );

setListAdapter(adapter);

}

然后我使用onclick监听器使用以下代码获取数据:

setContentView(R.layout.listview);


final ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {


    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        Toast.makeText(arg1.getContext(),
                getString(R.string.selected) + " " + position,
                Toast.LENGTH_SHORT).show();

        // How do I return the entire data that I used                  
        }
});

我可以在上面看到如何获取数组中数据的位置 - 但是如何返回视图中显示的实际结果数据?

1 个答案:

答案 0 :(得分:0)

声明List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

全局和ListView的onItemClick()只是放线,

Map<String, Object> map = resourceNames.get(position);