2016-12-07 65 views
0

我正在使用volley进行web服务。所以,当我每次运行应用程序时都不会在列表中显示任何内容。但是当我使用调试时,有时我正确地得到了列表。所以这里是代码。Android - json列表视图

这就像在从json获取数据之前插入listItem在Listview之前,我试图在适配器之前放置一个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

在响应呼叫notifydatasetchange凌空();方法 – Redman

+1

尝试从Web服务接收数据后刷新适配器。将新项目添加到'ArrayList'后,使用'adapter.notifyDataSetChanged()'。 –

+0

我添加了一个答案,以方便您 – Redman

回答

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

      } 
+0

mschedule在on响应之后被声明,如果我之前声明并将其作为final,它仍然不起作用 –