在AlertDialog中显示ArrayList

时间:2016-08-25 17:43:58

标签: android arraylist alertdialog

我从数据库中检索状态列表。我以JSON的形式获得了列表 我在ArrayList<String>中添加了所有状态 现在,我想使用适配器在AlertDialog中显示完整的状态列表。

但是当我将ArrayList设置为Adapter并在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();
    }
});

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);
}

每次循环时都会创建一个新的数组适配器。把它拿出循环让我知道它是否有效! :)

答案 2 :(得分: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();
                    }
                });