ArrayList不会填充ListView,但是我可以显示添加到ArrayList的项目

时间:2018-06-20 13:10:11

标签: android arraylist

我不知道为什么添加到ArrayList的项目不会填充ListView,我尝试检查是否确实将项目添加到ArrayList,但它确实显示了。

您可以在此处查看已添加项目的图像

enter image description here

这是我的代码。我为checkedtextview使用了另一个xml,因此我的列表视图将显示带有复选框的项目。

ArrayList<String> symptomsListTest = new ArrayList<>();
ListView chl1;
String URL = "***********";
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    addListItem();
    chl1 = (ListView) findViewById(R.id.checklistSample);

    chl1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.symptoms_checklist, R.id.txt_title, symptomsListTest);
    //ArrayAdapter<String> aa=new ArrayAdapter<String>(this,R.layout.symptoms_checklist, R.id.txt_title,symptomsListTest);
    chl1.setAdapter(aa);

    chl1.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            String selectedItem = ((TextView) view).getText().toString();
            if(symptomsListTest.contains(selectedItem))
            {
                symptomsListTest.remove(selectedItem); //remove deselected item from the list of selected items
            }
            else
            {
                symptomsListTest.add(selectedItem); //add selected item to the list of selected items
            }
        }
    });
}

public void addListItem()
{
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        String message = jsonObject.getString("message");
                        JSONArray jsonArray = jsonObject.getJSONArray("read");

                        if(success.equals("1"))
                        {
                            for(int i = 0; i < jsonArray.length(); i++)
                            {
                                JSONObject object = jsonArray.getJSONObject(i);
                                String symptom = object.getString("symptom1");

                                symptomsListTest.add(symptom);
                                //Toast.makeText(SAMPLE.this, ""+symptom, Toast.LENGTH_SHORT).show();

                                //Toast.makeText(Login.this,  ""+message+"\nYour name is: "+name+"\nYour email is: "+email,Toast.LENGTH_SHORT).show();
                            }
                        }
                        else
                        {
                            Toast.makeText(SAMPLE.this, ""+message,Toast.LENGTH_SHORT).show();
                        }
                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                        Toast.makeText(SAMPLE.this, e.toString(),Toast.LENGTH_SHORT).show();
                    }

                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    Toast.makeText(SAMPLE.this, error.toString(),Toast.LENGTH_SHORT).show();
                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError
        {
            Map<String, String> params = new HashMap<>();
            String result="success";
            params.put("result", result);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

1 个答案:

答案 0 :(得分:0)

您正在执行一个AsyncTask,该AsyncTask会在一段时间后给出响应,同时listView及其适配器的代码已经执行。您只需要在for循环之后添加一行,就像这样:-

 for (int i = 0; i < jsonArray.length(); i++) {
       JSONObject object = jsonArray.getJSONObject(i);
       String symptom = object.getString("symptom1");
       symptomsListTest.add(symptom);
       //Toast.makeText(SAMPLE.this, ""+symptom, Toast.LENGTH_SHORT).show();
       //Toast.makeText(Login.this,  ""+message+"\nYour name is: "+name+"\nYour email is: "+email,Toast.LENGTH_SHORT).show();
    }
    //** Add This line **
    aa.notifyDataSetChanged();

并确保将ArrayAdapter声明为全局。希望这会有所帮助。