的项目的catName
如何从GridView中获得要传递给我的意图的正确值。看来我做这件事的方式不对。我是这么做的:
在我的主要活动中:
CategoryRowItem category;
.
.
.
//after parsing from json
category = new CategoryRowItem(catId, catName);
categoryItems.add(category);
gridView = (GridView) findViewById(R.id.grid_view);
final CustomBaseAdapter adapter = new CustomBaseAdapter(this, categoryItems);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String categoryId = category.getCatId(); // i think this not right
String categoryName = category.getCatName();
System.out.println("CAT ID: " +categoryId);
Intent intent = new Intent(getApplicationContext(), ShopListActivity.class);
intent.putExtra("catID", categoryId);
intent.putExtra(CATEGORY, categoryName);
startActivity(intent);
}
});
我的CategoryRowItem类:这有getter和setter
public String getCatId(){
return catId;
}
public String getCatName(){
return catName;
}
public void setCatId(String cId){
this.catId = cId;
}
public void setCatName(String name){
this.catName = name;
}
那么,我如何才能获得正确的值,如catID,或点击>
position
会告诉您列表中的哪个项目被点击/选中,使用它您可以获得CategoryRowItem
及其详细信息。
CategoryRowItem category = categoryItems.get(position); // Add this line here.
String categoryId = category.getCatId(); // Now this will be right.
String categoryName = category.getCatName();
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String categoryId = categoryItems.get(position).getCatId(); // i think this not right
String categoryName = categoryItems.get(position).getCatName();
System.out.println("CAT ID: " +categoryId);
Intent intent = new Intent(getApplicationContext(), ShopListActivity.class);
intent.putExtra("catID", categoryId);
intent.putExtra(CATEGORY, categoryName);
startActivity(intent);
}
});
将此代码替换为网格侦听器