0

如何从我的 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>

4

2 回答 2

1

position告诉您单击/选择了列表中的哪个项目,并使用它可以获取您CategoryRowItem的详细信息及其详细信息。

CategoryRowItem category = categoryItems.get(position); // Add this line here.
String categoryId = category.getCatId(); // Now this will be right.
String categoryName = category.getCatName();
于 2013-03-12T10:21:54.393 回答
1
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);
    }
});

用您的网格侦听器替换此代码

于 2013-03-12T10:24:11.920 回答