通过菜单项android传递id

时间:2017-12-23 06:19:56

标签: android menuitem

我正在动态地在导航栏中加载菜单项。在setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()的帮助下,我试图从数据库中获取并传递id到下一个活动,但是当我点击一个菜单项时,它会显示最新的ID,然后再显示第一个ID。我该如何做到这一点?

 public void addDynamic() {
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(final String response) {

                    try {
                        jsonObject = new JSONObject(response);
                        jsonArray = jsonObject.getJSONArray("list");
                        submenu = menu.addSubMenu("Test");
                        for (int i = 0; i <jsonArray.length(); i++) {
                            jsonObject1 = jsonArray.getJSONObject(i);
                            a = submenu.add(jsonObject1.getString("name"));
                            a.setIcon(new IconicsDrawable(MainActivity.this)
                                    .icon(FontAwesome.Icon.faw_flask)
                                    .sizeDp(24));
                            a.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                                @Override
                                public boolean onMenuItemClick(MenuItem item) {
                                    try {
                                        JSONObject object = new JSONObject(response);
                                        JSONArray array = object.getJSONArray("list");
                                       for (int i = 0; i <array.length(); i++) {
                                            JSONObject object1 = array.getJSONObject(i);
                                            id = object1.getString("id");
                                            Bundle b = new Bundle();
                                            b.putString("id", id);
                                            Intent intent = new Intent(MainActivity.this, TestDetails.class);
                                            intent.putExtra("fid", b);
                                            startActivity(intent);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                        return false;
                                }
                            });
                        }
                    }
                    catch (JSONException e) {
                        Log.e("Error", "Failed" + e.toString());
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Error", "Try Later" + error.toString());
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    requestQueue.add(stringRequest);
}

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(IconicsContextWrapper.wrap(newBase));
}

2 个答案:

答案 0 :(得分:0)

intent.putExtra()中的

使用id的字符串值,如下所示

 JSONObject object1 = array.getJSONObject(i);
 id = object1.getString("id");
 //Bundle b = new Bundle(); remove this line
 //b.putString("id", id); also remove this line
 Intent intent = new Intent(MainActivity.this, TestDetails.class);
 intent.putExtra("fid", id);

用于获取意图包值,请尝试以下代码

    final Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if(bundle != null){
        String FID = bundle.getString("fid");
     }

答案 1 :(得分:0)

你在这做什么???

public boolean onMenuItemClick(MenuItem item) {
                                try {
                                    JSONObject object = new JSONObject(response);
                                    JSONArray array = object.getJSONArray("list");
                                   for (int i = 0; i <array.length(); i++) {
                                        JSONObject object1 = array.getJSONObject(i);
                                        id = object1.getString("id");
                                        Bundle b = new Bundle();
                                        b.putString("id", id);
                                        Intent intent = new Intent(MainActivity.this, TestDetails.class);
                                        intent.putExtra("fid", b);
                                        startActivity(intent);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                    return false;
                            }

请注意循环!如果你的循环执行多次,那会发生什么?假设数组长度= 2然后它将迭代两次并且只需单击一个项目它就会启动两个活动,其中包含值为[id]的值,这意味着

                                    Bundle b = new Bundle();
                                    b.putString("id", id);
                                    Intent intent = new Intent(MainActivity.this, TestDetails.class);
                                    intent.putExtra("fid", b);
                                    startActivity(intent);

这些代码将执行两次并连续启动两个活动,您将看到最后一个活动。首先解决这个问题可能会解决您的问题。添加一个条件就好像只需一次点击就可以启动一个活动。