如何从适配器位置获取值,我的代码如下:
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.
}
});
如果我在MyLog中将其记录在logcat上,我得到:
价值是:{position = 12, IMAGE_PATH = http://192.168.103.121/xml/icon.png, link = http://192.168.103.121/xml/category.php?kat_id=13,name = Category 13}
所以我的问题,我想从name获取值并存储到变量String name,我想在String名称中只获得“Category 13”。因为我想把它传递给另一个活动。
答案 0 :(得分:34)
看起来你用hashmap创建了对象,比如blablabla.put(“name”,“value”)??如是。试试这个:
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);
}
});
答案 1 :(得分:6)
value = value.substring(value.indexOf("name=")+5,value.length()-1);
答案 2 :(得分:3)
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();
}
});
答案 3 :(得分:0)
您还可以通过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();
});
}
答案 4 :(得分:0)
这就是我所做的: -
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());
}
});
玩得开心!