2017-12-23 2 views
1

탐색함에 메뉴 항목을 동적으로로드하고 있습니다. setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()의 도움으로 데이터베이스에서 id을 다음 활동으로 가져오고 전달하려하지만 가장 최근 ID를 표시 한 다음 다시 1 번째 ID를 표시하는 메뉴 항목을 클릭합니다. 이 권리는 어떻게 얻을 수 있습니까? 의도 번들 값을 페치 메뉴 항목을 통해 ID 전달 android

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

아래

같은 idintent.putExtra() 사용 문자열 값에

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)); 
} 
+0

왜 for 루프에서 ** 의도 **를 사용하고 있습니까? – Omi

+0

a.setOnMenuItemClickListene이 줄에서 a의 데이터 형식은 무엇이며 전역 값입니까 ?? json에서 구문 분석 한 후 로컬 값을 작성하고 리스너를 추가해야합니다. 리스너는 사용자가 작성한 모든 항목에 해당합니다. 메뉴 항목 –

+0

@ Masum Biswas a는 전역으로 선언 된 menuitem입니다. 너 좀 자세히 설명해 줄 수 있니? 나는 초보자이다. –

답변

0

당신이 여기 뭐하는 코드

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

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

Intent intent = getIntent(); 번들 b = intent.getBundleExtra ("fid"); id = b.getString ("id"); 다음 활동에서 가져 오는 올바른 방법입니까? –

+0

@AparnaMutnalkar 다음 활동 점검에서 업데이트 된 응답 확인 ... – Omi

+0

확인 감사합니다. 비록 루프에서 의도를 넣었더라도 문제는 여전히 동일합니다. 활동이 두 번 열리고 두 ID가 다음 의도로 갈 것입니다. for 루프가 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; 
          } 

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

이러한 코드가 두 번 실행 연속적 두 가지 활동을 시작 의미 마지막 활동을 보게 될 것입니다. 먼저이 문제를 해결하면 문제가 해결 될 수 있습니다. 한 번의 클릭으로 하나의 활동 만 시작되는 것처럼 조건을 추가하십시오.

+0

for 루프에서 인 텐트를 꺼내지 만 여전히 두 번 실행됩니다. –

관련 문제