我正在动态加载导航抽屉中的菜单项。在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));
}
发布于 2017-12-23 06:22:29
在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");
}
发布于 2017-12-23 06:35:00
你在这里做什么?
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;
}
请注意for循环!如果你的循环执行多次,那么会发生什么呢?假设数组长度=2,那么它将迭代两次,对于一次单击项目,它将启动两个活动,其中有一个受尊重的值id,这意味着
Bundle b = new Bundle();
b.putString("id", id);
Intent intent = new Intent(MainActivity.this, TestDetails.class);
intent.putExtra("fid", b);
startActivity(intent);
这些代码将执行两次,并连续启动两个活动,您将看到最后一个活动。首先解决这个问题,你的问题可能会解决。添加一个条件,就好像您的一次单击只会启动一个活动。
https://stackoverflow.com/questions/47950274
复制相似问题