2016-02-08 35 views
0

问题是有时列表没有显示,我的网络连接没有问题。 我尝试了几次运行该应用程序。 1/10运行,列表在那里,我可以看到数据库中的数据。我真的不知道我用凌空获取数据Android的ListView没有显示,但从JSON的数据是

package com.example.wackyroad.internannouncement; 


public class MainActivity extends ListActivity { 

private static final String GET_URL = "http://XXX"; 
private static final String TAG_ID = "id"; 
private static final String TAG_TITLE = "title"; 
private static final String TAG_CONTENT = "content"; 
private static final String TAG_DATE = "date"; 
private ArrayList<HashMap<String, String>> listArrayList = new ArrayList<HashMap<String, String>>(); 
private ProgressDialog pDialog; 
private ListAdapter adapter; 

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

    getAnnouncement(); 
    updateList(); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.refresh) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


private void getAnnouncement() { 
    StringRequest postRequest = new StringRequest(Request.Method.POST, GET_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        try { 
         JSONObject jsonResponse = new JSONObject(response); 

         try { 
          JSONArray data = jsonResponse.getJSONArray("announcements"); 

          for (int i = 0; i < data.length(); i++) { 
           JSONObject c = data.getJSONObject(i); 

           HashMap<String, String> map = new HashMap<String, String>(); 
           map.put(TAG_ID, c.getString("announcement_id")); 
           map.put(TAG_TITLE, c.getString("announcement_title")); 
           map.put(TAG_CONTENT, c.getString("announcement_content")); 
           map.put(TAG_DATE, c.getString("announcement_date")); 
           listArrayList.add(map); 
          } 

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

         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        pDialog.dismiss(); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        error.printStackTrace(); 
        pDialog.dismiss(); 
       } 
      } 

    ) { 
    }; 

    pDialog = new ProgressDialog(MainActivity.this); 
    pDialog.setMessage("Getting Announcements.."); 
    pDialog.show(); 

    Volley.newRequestQueue(getApplication()).add(postRequest); 

} 

private void updateList() { 
    adapter = new SimpleAdapter(this, listArrayList, 
      R.layout.activity_lv_list, new String[]{TAG_ID, TAG_TITLE, TAG_CONTENT, 
      TAG_DATE}, new int[]{R.id.tv_id, R.id.tv_title, R.id.tv_content, R.id.tv_date}); 


    setListAdapter(adapter); 

    ListView lv = getListView(); 
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      String tv_id = ((TextView) view.findViewById(R.id.tv_id)).getText().toString(); 
      Intent i = new Intent(getApplicationContext(), Announcement_Details.class); 
      i.putExtra("id", tv_id); 
      startActivity(i); 

     } 
    }); 
} 


} 

我试图登录的jsonResponse问题 和数据实际上是牵强。 这里的jsondata

02-08 10:34:38.676 6258-6258/com.example.wackyroad.internannouncement D/JSONRESPONSE﹕ {"announcements":[{"announcement_title":"Sample Title Here","announcement_content":"Sample Content","announcement_id":"1","announcement_date":"2016-02-04"},{"announcement_title":"Sample Title","announcement_content":"Sample Content Again","announcement_id":"2","announcement_date":"2016-02-04"},{"announcement_title":"It's not working","announcement_content":"I really don't know","announcement_id":"3","announcement_date":"2016-02-08"}],"message":"Post Available!","success":1} 

我认为这个问题是数据的列表视图中

回答

4

getAnnouncement(人口)是异步操作。 您必须在完成请求后调用updateList()方法。

+1

我明白了,所以这就是为什么有些时候它的表现有时不是。感谢队友,我没有看到。 – Alvinrightback

0

调用updateList()后更新的HashMap像

      try { 
          JSONArray data = jsonResponse.getJSONArray("announcements"); 

          for (int i = 0; i < data.length(); i++) { 
           JSONObject c = data.getJSONObject(i); 

           HashMap<String, String> map = new HashMap<String, String>(); 
           map.put(TAG_ID, c.getString("announcement_id")); 
           map.put(TAG_TITLE, c.getString("announcement_title")); 
           map.put(TAG_CONTENT, c.getString("announcement_content")); 
           map.put(TAG_DATE, c.getString("announcement_date")); 
           listArrayList.add(map); 
          } 
          updateList(); 
         } catch (Exception e) { 
          e.printStackTrace(); 

         }