如何从适配器位置获得价值,我有代码如下:
CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
List<HashMap<String, Object>> categories = null;
try {
categories = categoryXmlParser.parse(reader);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
String[] from = { "name", "image" };
int[] to = { R.id.nama_category, R.id.logo_category };
final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
categories, R.layout.per_item_category, from, to);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
如果我期待登录它的logcat在MyLog我得到如下:
值是:{位置= 12,IMAGE_PATH = http://192.168.103.121/xml/icon.png ,链接= http://192.168.103.121/xml/category.php?kat_id=13 ,名字=类别13}
所以我的问题,我想从名称值并存储到变量字符串的名字,我想只是“类别13”中的字符串名称。 因为我想将它传递给另一个活动。
看起来你做出与HashMap中的对象,这样的blablabla.put(“名”,“值”)? 如是。 试试这个:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
改成:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String name = (String) obj.get("name");
Log.d("Yourtag", name);
}
});
value = value.substring(value.indexOf("name=")+5,value.length()-1);
listID.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
Object obj=listID.getAdapter().getItem(position);
String str=obj.toString();
Toast.makeText(MainActivity.this, "Item is= "+str, Toast.LENGTH_LONG).show();
}
});
您也可以通过获取适配器数据AdapterView<?> parent
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//setHasOptionsMenu(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final Filedata mnotes = (Filedata) parent
.getItemAtPosition(position);
Toast.makeText(getActivity(), Filedata.getName(),Toast.LENGTH_SHORT).show();
});
}
这就是我所做的: -
lvHomePage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Transaction selectedFromList =(Transaction)parent.getAdapter().getItem(position);
Log.d("val:",selectedFromList.getDate());
Log.d("val:",selectedFromList.getMobile_no());
}
});
玩得开心!