【问题标题】:How to get data of hashmap of custom gridview adapter when clicked on gridview item?单击gridview项目时如何获取自定义gridview适配器的hashmap数据?
【发布时间】:2017-02-14 04:51:26
【问题描述】:

在我的应用程序中,我尝试使用 ArrayList<HashMap<String, String>> 保存数据 以下是我的代码:

 JsonArrayRequest req = new JsonArrayRequest(Constants.DARSHAN_URL+Constants.MAIN_CATEGORY,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object
                            for (int i = 0; i < response.length(); i++) {

                                JSONObject person = (JSONObject) response.get(i);

                                String material_id = person.getString("material_id");
                                String material_desc = person.getString("material_desc");

                                HashMap<String, String> maim_category_array = new HashMap<>();

                                maim_category_array.put("material_id", material_id);
                                maim_category_array.put("material_desc", material_desc);

                                categoryName.add(maim_category_array);

                            }

                            adapter = new GridviewAdapter(MainCategoryActivity.this, categoryName);
                            grid.setAdapter(adapter);

下面是我的 GridViewCoustomAdapter:

public class GridviewAdapter extends BaseAdapter
{
     private ArrayList<HashMap<String, String>> listCountry;
    private Activity activity;

    public GridviewAdapter(Activity activity, ArrayList<HashMap<String, String>> listCountry) {
        super();
        this.listCountry = listCountry;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listCountry.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.button_layout, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.btn_select);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> map = new HashMap<String, String>();
        map = listCountry.get(position);
        view.txtViewTitle.setText(map.get("material_desc"));

        return convertView;
    }}

现在我想获取 material_Id 以发送到下一个活动。即我想获取gridview的matrial_Id onItemClick 例如:

grid.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                                Toast.makeText(MainCategoryActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
                                Intent i = new Intent(MainCategoryActivity.this, ProductNameActivity.class);

                                i.putExtra("MATERIAL_ID", <material_id>);
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i);
                            }
                        });

我该怎么做??

请帮忙!!

【问题讨论】:

    标签: android gridview arraylist hashmap


    【解决方案1】:

    onItemClick

    HashMap<String, String> map = (HashMap<String, String>)arg0.getItemAtPosition(position);
    

    然后

    String id = map.get("material_id");
    

    【讨论】:

    • HashMap 处的“预期表达式”
    • 完美解决方案...非常感谢..!! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多