通过菜单项android传递id

问题描述 投票:1回答:2

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

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


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");
     }
© www.soinside.com 2019 - 2024. All rights reserved.