使用arraylist填充setMulitiChoiceItems不起作用

时间:2018-06-04 05:50:57

标签: java android json android-studio android-alertdialog

我一直在尝试使用ArrayList填充setMultiChoiceItems,但它无法正常工作。

我实际上是在尝试从JSON中提取数据并将其显示在一个对话框中,作为我可以将数据发送到服务器的清单。

我已经完成了我需要从JSON读取的部分,并且工作正常。但是当我将ArrayList转换为Array时,它不会显示在列表中。如果可能的话,请给我一个提示,用多个按钮管理相同的代码及其各自的对话框。

我的代码如下:

public class MainActivity extends AppCompatActivity {

    TextView textView;

    static String[] teams = {"man","RM", "Bar", "city"};
    AlertDialog ad;
    Button goBtn;
    ArrayList<Integer> selected = new ArrayList<>();

    boolean[] checkedItems;


    private  static final String URL = "https://jsonplaceholder.typicode.com/posts";
    List<String> list = new ArrayList<String>();
    String[] listItems;

    String TAG = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        populateList();
        goBtn = findViewById(R.id.goBtn);
        textView = findViewById(R.id.textView);
        goBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.show();


            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Teams");
        //builder.setItems(teams, null);

        builder.setNegativeButton("Cancel",null);
        builder.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(getApplicationContext(), listItems[which], Toast.LENGTH_SHORT).show();
                if(!selected.contains(which)){
                    selected.add(which);
                    Log.i(TAG, "onClick: added " + listItems[which]);
                }
                else{
                    selected.remove(which);
                    Log.i(TAG, "onClick: deleted "+ listItems[which]);
                }
            }
        });
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {



            }
        });
        ad = builder.create();
        //ad.show();
    }

    private void populateList(){
        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try{
                    //Log.i(TAG, "onResponse: "+response);
                    JSONArray data = new JSONArray(response);
                    for(int i = 0; i< data.length(); i++){
                        JSONObject innerObj = data.getJSONObject(i);
                        String name = innerObj.getString("title");
                        list.add(name.substring(0,5));
                        if(i==7){
                            break;
                        }
                        //Log.i(TAG, "onResponse: "+list.get(i));
                        //Log.d(TAG, "onResponse: "+name.substring(0,5));
                    }
                    listItems = new String[list.size()];
                    listItems = list.toArray(new String[list.size()]);
                    /*for(int i = 0; i < list.size(); i++){
                        listItems[i] = list.get(i);
                        Log.i(TAG, "onResponse: "+listItems[i]);
                        //textView.append(list.get(i));
                    }*/

                    checkedItems = new boolean[listItems.length];
                    Log.i(TAG, "onResponse: SIZEE"+listItems.length);
                    Log.i(TAG, "onResponse: "+checkedItems.length);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }
}

0 个答案:

没有答案