【问题标题】:Android - json list viewAndroid - json 列表视图
【发布时间】:2016-12-07 08:25:33
【问题描述】:

我将 volley 用于 Web 服务。因此,当我每次运行应用程序时,它都不会在列表中显示任何内容。但是当我使用调试时,有时我得到了正确的列表。所以这里是代码。

就像它在从 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);

        }
    }

【问题讨论】:

  • 响应调用 notifydatasetchange();方法
  • 在收到来自 web 服务的数据后尝试刷新适配器。向ArrayList 添加新项目后使用adapter.notifyDataSetChanged()
  • 我添加了一个答案,方便您使用

标签: android json listview android-volley


【解决方案1】:

将 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();
                }

            }

【讨论】:

  • mschedule 是在 on 响应之后声明的,如果我之前声明它并把它作为 final ,它仍然不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多