2012-09-30 80 views
-1

我想創建一個listView,其中JSON數據被拉入SimpleAdapter,但我能弄清楚如何隱藏Rank圖像,如果它返回null。Android ListView帶或不帶圖像

 private class LoadDataTask extends AsyncTask<Void, Void, String[]> { 
    ProgressDialog Dialog = new ProgressDialog(Post.this); 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mylist.clear(); 
     Dialog.setMessage("Loading Posts..."); 
     Dialog.setCancelable(true); 
     Dialog.show(); 
    } 
    @Override 
    protected String[] doInBackground(Void... params) { 
     preferences = PreferenceManager.getDefaultSharedPreferences(Post.this); 
     String userID = preferences.getString("userID", "n/a"); 
     String TID = Post.this.getIntent().getExtras().getString("id"); 
     try{ 
      JSONObject json = JSONfunctions.getJSONfromURL(Constants.BASE_URL+"/posts.php?user="+userID+"&postID="+TID); 
      JSONArray users = json.getJSONArray("users"); 
      for(int i=0;i<users.length();i++){ 
      HashMap<String, String> map = new HashMap<String, String>();  
      JSONObject e = earthquakes.getJSONObject(i); 
      map.put("pid", e.getString("poster_id")); 
      map.put("id", e.getString("post_id")); 
       map.put("Forum", e.getString("forum_id")); 
       map.put("Topic", e.getString("topic_id")); 
       map.put("Name", e.getString("poster_name")); 
       map.put("Text", string); 
       map.put("IP", e.getString("poster_ip")); 
       map.put("Status", e.getString("poster_status")); 
       map.put("Rank", e.getString("prank")); 
       map.put("Time", e.getString("poster_time")); 
       map.put("active", e.getString("active")); 
       map.put("posterisimage", e.getString("posterisimage")); 
       map.put("posterimage", e.getString("posterimage")); 
       mylist.add(map);    
      }  
     }catch(JSONException e)  { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String[] result) { 
    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
      new String[] { "Time", "Text", "Name" ,"Rank"}, <==Hide Rank if returned null ??? 
      new int[] { R.id.item_date, R.id.item_subtitle , R.id.item_title, R.id.item_image}); 
    ((SimpleAdapter) adapter).notifyDataSetChanged(); 
    setListAdapter(adapter); 
     Dialog.dismiss(); 
     super.onPostExecute(result); 
    } 
} 

有人可以顯示或指出正確的方向。

回答

0

您可以從SimpleAdapter寫你自己的ViewBinder:

adapter.setViewBinder(new SimpleAdapter.ViewBinder() { 
    @Override 
    public boolean setViewValue(View view, Object data, String textRepresentation) { 
     if(view.getId() != R.id.item_image) 
      return false; 

     if(data == null) { 
      view.setVisibility(View.GONE); 
      return true; 
     } 

     view.setVisibility(View.VISIBLE); 
     return false; 
    } 

}); 

這ViewBinder很原始。我建議根據您的特定佈局進行定製。