通过菜单项Android传递ID



我正在动态地加载导航抽屉中的菜单项。在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));
}

您在这里做什么?

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],这是emand

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

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

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");
     }

最新更新