2016-08-25 52 views
0

我從我的數據庫中檢索一個狀態列表。我以JSON的形式獲得列表。
我將所有狀態添加到ArrayList<String>
現在我想通過使用適配器在AlertDialog中顯示完整的狀態列表。在AlertDialog中顯示一個ArrayList

但是,當我將ArrayList設置爲適配器並在builder.setAdapter();中使用該適配器時,它顯示了我的ArrayList的最後一項;即:完整ArrayList中只有一個值。

我不知道該怎麼辦?

下面

是我的代碼

JSONObject jsonObject = new JSONObject(response.body().string()); 
Log.d("JSON", String.valueOf(jsonObject)); 
JSONArray jsonArray = jsonObject.getJSONArray("states"); 

for(int i=0; i<jsonArray.length(); i++) { 
    stateList = new ArrayList<String>(); 
    JSONObject jobjstate = jsonArray.getJSONObject(i); 

    states = jobjstate.getString("state_name"); 

    stateList.add(states); 
    stateAdapter = new ArrayAdapter<String>(UserRegister.this, android.R.layout.simple_spinner_dropdown_item, stateList); 
} 

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(UserRegister.this); 

     View customTitle = View.inflate(getApplicationContext(), R.layout.custom_dialog_state, null); 
     builder.setCustomTitle(customTitle); 

     builder.setAdapter(stateAdapter, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       etregstate.setTextColor(getResources().getColor(R.color.black)); 
       etregstate.setText(stateList.get(which)); 
       dialog.dismiss(); 
      } 
     }); 
     AlertDialog alertDialog = builder.create(); 
     ListView listView = alertDialog.getListView(); 
     listView.setDivider(new ColorDrawable(getResources().getColor(R.color.purple))); 
     listView.setDividerHeight(2); 
     alertDialog.show(); 
    } 
}); 
+1

你的問題是,你可以通過創建for循環,每次一個項目的品牌新的適配器。這不是一個Android問題 –

回答

1

希望這能解決問題

stateList = new ArrayList<String>(); 

for(int i=0; i<jsonArray.length(); i++) { 
    JSONObject jobjstate = jsonArray.getJSONObject(i); 
    states = jobjstate.getString("state_name"); 
    stateList.add(states); 
} 

stateAdapter = new ArrayAdapter<String>(UserRegister.this, android.R.layout.simple_spinner_dropdown_item, stateList); 
+0

我嘗試這樣做。它不能解決我的問題 –

+0

爲什麼不呢?它完美地描述了以前只顯示一個元素的事實。那麼,你能否更清楚問題是什麼? –

+0

是的你是對的。我再次嘗試了一些與更改相同的解決方案。我把我的stateAdapter作爲final。並從循環中創建ArrayAdapter。它的工作原理。謝謝。 –

0

我認爲這個問題是在這裏

for(int i=0; i<jsonArray.length(); i++) { 
    stateList = new ArrayList<String>(); 
    JSONObject jobjstate = jsonArray.getJSONObject(i); 

    states = jobjstate.getString("state_name"); 

    stateList.add(states); 
    /* This line */ 
    stateAdapter = new ArrayAdapter<String>(UserRegister.this, android.R.layout.simple_spinner_dropdown_item, stateList); 
} 

每一次當你創建一個新的陣列適配器你循環。只要把它從循環中拿出來,讓我知道它是否有效! :)

+0

It Works。循環後我必須創建stateAdapter。我理解這個問題。經過一些改變它的工作。謝謝 –

0

我問上述問題。經過一些改變,我得到了解決方案。我張貼我的解決方案代碼爲其他人的幫助。

謝謝。

JSONObject jsonObject = new JSONObject(response.body().string()); 
       Log.d("JSON", String.valueOf(jsonObject)); 

       success = jsonObject.getInt(MateAppsConstants.TAG_SUCCESS); 

       Log.d("Success", String.valueOf(success)); 

       if (success == 1) { 
        JSONArray stateJSON = null; 
        stateJSON = jsonObject.getJSONArray(MateAppsConstants.TAG_STATE); 

        stateList = new ArrayList<String>(); 

        for (int i=0; i<stateJSON.length(); i++) { 
         JSONObject jobjstate = stateJSON.getJSONObject(i); 
         states = jobjstate.getString(MateAppsConstants.TAG_STATENAME); 
         Log.d("State List", states); 

         if (states != null) 
          stateList.add(states); 
        } 
       } else { 
        msg = jsonObject.getString(MateAppsConstants.TAG_MESSAGE); 
        Log.d("No State Found", msg); 
       } 

       final ArrayAdapter<String> stateAdapter = new ArrayAdapter<String>(UserRegister.this, android.R.layout.simple_spinner_dropdown_item, stateList); 

       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         AlertDialog.Builder builder = new AlertDialog.Builder(UserRegister.this); 
         View customTitle = View.inflate(getApplicationContext(), R.layout.custom_dialog_state, null); 
         builder.setCustomTitle(customTitle); 

         builder.setAdapter(stateAdapter, new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           etregstate.setTextColor(getResources().getColor(R.color.black)); 
           etregstate.setText(stateList.get(which)); 
           dialog.dismiss(); 
          } 
         }); 
         AlertDialog alertDialog = builder.create(); 
         ListView listView = alertDialog.getListView(); 
         listView.setDivider(new ColorDrawable(getResources().getColor(R.color.purple))); 
         listView.setDividerHeight(2); 
         alertDialog.show(); 
        } 
       });