0

我正在尝试使用 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                  
        }
});

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

4

1 回答 1

0

宣布List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

全局并在 ListView 的onItemClick()just put 行中,

Map<String, Object> map = resourceNames.get(position);
于 2012-04-21T07:32:35.110 回答