我從我的數據庫中檢索一個狀態列表。我以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();
}
});
你的問題是,你可以通過創建for循環,每次一個項目的品牌新的適配器。這不是一個Android問題 –