-1

我正在尝试创建一个 listView,其中 JSON 数据被拉入 SimpleAdapter,但我能弄清楚的是如何隐藏 Rank 图像,如果它返回 null。

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

有人可以向我展示或指出正确的方向吗?

4

1 回答 1

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 非常原始。我建议将其自定义为您的特定布局。

于 2012-09-30T19:40:33.430 回答