如何从我的 GridView 获取正确的值以传递给我的意图。看来我做错了。所以这就是我所做的:
在我的 MainActivity 中:
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 或单击的项目的 catName>