Reputation: 11
I retrieve a state list from my database. I get the list in the form of JSON.
I add all the states in an ArrayList<String>
.
Now I want to display that complete list of states in an AlertDialog by using an Adapter.
But when I set ArrayList to an Adapter and use that Adapter in builder.setAdapter();
, it displays the last item of my ArrayList; i.e.: only one value out of the complete ArrayList.
I don't know how to do?
below is my code
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();
}
});
Upvotes: 0
Views: 2266
Reputation: 11
I ask the above question. And After some changes I got the solution. I am posting my solution code for other's help.
Thank you.
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();
}
});
Upvotes: 0
Reputation: 804
I think the problem is here
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);
}
You're creating a new array adapter every time you loop. Just take it out of the loop and let me know if it works! :)
Upvotes: 0
Reputation: 191854
Hopefully this fixes the problem
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);
Upvotes: 1