Android - json列表视图

时间:2016-12-07 08:25:34

标签: android json listview android-volley

我正在使用排球进行网络服务。所以,当我每次运行应用程序时它都没有在列表中显示任何内容。但是当我使用调试时,有时候我正确地得到了列表。所以这是代码。

就像在从json获取数据之前在Listview上插入listItem一样,我试图在适配器之前放置一个wait()但是android studio不接受它。

有没有人有类似的问题?

    public class listedeal_res extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listedeal_res);
            Button ajouter=(Button) findViewById(R.id.ajouetres);
            Intent i =getIntent();
            final   ListView maListView = (ListView) findViewById(R.id.liste_deal_res);
            final int idres=i.getIntExtra("idres",0);
            final boolean b =true;
            ajouter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(listedeal_res.this,AjouterDeal.class);
                    intent.putExtra("idres",idres);
                    listedeal_res.this.startActivity(intent);
                }
            });
           final ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

          final  HashMap<String, String> map;
            map = new HashMap<String, String>();
            Response.Listener<String> resp=new  Response.Listener<String>()
            {           
                @Override
                public void onResponse (String response) {
                    try {       
                        JSONArray jsonResponse = new JSONArray(response);
                        int i=0;    
                        while (jsonResponse.getJSONArray(i)!=null)
                        {
                            while (jsonResponse.getJSONArray(i)!=null)
                            {
                                map.put("Nom",jsonResponse.getJSONArray(i).getString(2));
                                map.put("Restraurant",jsonResponse.getJSONArray(i).getString(0));
                                map.put("description",jsonResponse.getJSONArray(i).getString(1));
                                listItem.add(map);

                                i++;
                            }
                        }    
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            };

            drinkeat.drinkandeat.javas.liste_deal_res_req liste_deal_res_req= new liste_deal_res_req(idres+"",resp);
            RequestQueue queue= Volley.newRequestQueue(listedeal_res.this);
            queue.add(liste_deal_res_req );

            SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.row_deal_user,
                    new String[] {"Nom", "Restraurant", "description"}, new int[] {R.id.titlerow, R.id.resteaurantnaamerow, R.id.descriptionrow});

            maListView.setAdapter(mSchedule);

        }
    }

1 个答案:

答案 0 :(得分:0)

将notifydatasetchange方法添加到您的适配器,如下所示

 public void onResponse (String response) {
                try {       
                    JSONArray jsonResponse = new JSONArray(response);
                    int i=0;    
                    while (jsonResponse.getJSONArray(i)!=null)
                    {
                        while (jsonResponse.getJSONArray(i)!=null)
                        {
                            map.put("Nom",jsonResponse.getJSONArray(i).getString(2));
                            map.put("Restraurant",jsonResponse.getJSONArray(i).getString(0));
                            map.put("description",jsonResponse.getJSONArray(i).getString(1));
                            listItem.add(map);

                            i++;
                        }
                    } 
                     mSchedule.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }