2015-02-23 59 views
0

我有ListView,它接受ArrayAdapter。當共享圖像時,ArrayAdapter有一個ImageView,由Picasso(來自Square的圖書館)填充。但是,在帶有圖像的消息被共享並放置在該列表項中的ImageView之後,如果另一個消息(沒有圖像)被共享,那麼相同的先前圖像被填充到ImageView中,而不是空白的,因爲它應該是如果沒有圖像被共享。ImageView在列表中將不會清除之前的圖像

我研究並嘗試將ImageView設置爲空或透明,如果沒有共享圖像但無效。到目前爲止,我已經嘗試過:

sharedSpecial.setImageBitmap(null);sharedSpecial.setImageResource(0);但都沒有工作。

如果沒有共享當前圖像,如何「清除」ImageView以顯示ListView中的上一張圖像?

ArrayAdapter類設置給定的共享型圖像低於:

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> { 

private TextView countryName; 
private ImageView sharedSpecial; 

private MapView locationMap; 
private GoogleMap map; 

private List<OneComment> countries = new ArrayList<OneComment>(); 
private LinearLayout wrapper; 


String getSharedSpecialURL = null; 
String getSharedSpecialWithLocationURL = null; 

String specialsActionURL = "http://" + Global.getIpAddress() 
     + ":3000/getSharedSpecial/"; 

String specialsLocationActionURL = "http://" + Global.getIpAddress() 
     + ":3000/getSharedSpecialWithLocation/"; 

String JSON = ".json"; 

@Override 
public void add(OneComment object) { 
    countries.add(object); 
    super.add(object); 
} 

public DiscussArrayAdapter(Context context, int textViewResourceId) { 
    super(context, textViewResourceId); 
} 

public int getCount() { 
    return this.countries.size(); 
} 

public OneComment getItem(int index) { 
    return this.countries.get(index); 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    if (row == null) { 
     LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.message_list_item, parent, false); 
    } 

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper); 

    OneComment comment = getItem(position); 

    countryName = (TextView) row.findViewById(R.id.comment); 

    sharedSpecial = (ImageView) row.findViewById(R.id.sharedSpecial); 

    countryName.setText(comment.comment); 

    // Initiating Volley 
    final RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue(); 

    // Check if message has campaign or campaign/location attached 
    if (comment.campaign_id == "0" && comment.location_id == "0") { 

     sharedSpecial.setImageBitmap(null); 

    } else if (comment.campaign_id != "0" && comment.location_id != "0") { 

     // If both were shared 
     getSharedSpecialWithLocationURL = specialsLocationActionURL + comment.campaign_id + "/" + comment.location_id + JSON; 


     // GET JSON data and parse 
     JsonObjectRequest getCampaignLocationData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialWithLocationURL, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 

         // Parse the JSON: 
         try { 
          resultObject = response.getJSONObject("shared"); 

          imageObject = resultObject.getJSONObject("image"); 
          adImageURL = imageObject.getString("url"); 


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

         // Get and set image 
         Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(sharedSpecial); 
         sharedSpecial.setImageResource(0); 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.d("Error.Response", error.toString()); 
        } 
       } 
     ); 

     requestQueue.add(getCampaignLocationData); 


    } else if (comment.campaign_id != "0" && comment.location_id == "0") { 

     // Just the campaign is shared 
     getSharedSpecialURL = specialsActionURL + comment.campaign_id + JSON; 

     // Test Campaign id = 41 

     // GET JSON data and parse 

     JsonObjectRequest getCampaignData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialURL, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 

         // Parse the JSON: 
         try { 
          resultObject = response.getJSONObject("shared"); 

          imageObject = resultObject.getJSONObject("image"); 
          adImageURL = imageObject.getString("url"); 

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

         // Get and set image 
         Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(sharedSpecial); 
         sharedSpecial.setImageResource(0); 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.d("Error.Response", error.toString()); 
        } 
       } 
     ); 

     requestQueue.add(getCampaignData); 

     // Location set to empty 
    } 

    // If left is true, then yello, if not then set to green bubble 
    countryName.setBackgroundResource(comment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green); 
    wrapper.setGravity(comment.left ? Gravity.LEFT : Gravity.RIGHT); 

    return row; 
} 

} 

回答

0

。在你的getView方法的問題。您沒有將充氣的xml附加到該行。您在全局聲明組件(textview等),而最好創建一個可以附加的持有者類。

能不能請你爲你的適配器如下:

UPDATE 新增構造以列表的參數中。現在給你打電話適配器像這樣:

DiscussArrayAdapter adapter = new DiscussArrayAdapter(context, R.layout.message_list_item, countries); 

listView.setAdapter(adapter); 

那麼它的重要信任適配器notifyDataSetChanged

在活動中全局創建國家/地區列表和適配器,每當您添加/刪除列表中的項目時,只需調用adapter.notifyDataSetChanged();

適配器:

public class DiscussArrayAdapter extends ArrayAdapter<OneComment>{ 


private MapView locationMap; 
private GoogleMap map; 

private List<OneComment> countries; 

public DiscussArrayAdapter(Context context, int resource, List<OneComment> objects) { 
    super(context, resource, objects); 

    this.countries = objects; 
} 
String getSharedSpecialURL = null; 
String getSharedSpecialWithLocationURL = null; 

String specialsActionURL = "http://" + Global.getIpAddress() 
     + ":3000/getSharedSpecial/"; 

String specialsLocationActionURL = "http://" + Global.getIpAddress() 
     + ":3000/getSharedSpecialWithLocation/"; 

String JSON = ".json"; 

@Override 
public void add(OneComment object) { 
    countries.add(object); 
    super.add(object); 
} 

public DiscussArrayAdapter(Context context, int textViewResourceId) { 
    super(context, textViewResourceId); 
} 

public int getCount() { 
    return this.countries.size(); 
} 

public OneComment getItem(int index) { 
    return this.countries.get(index); 
} 

public View getView(int position, View row, ViewGroup parent) { 

    //Create instance of inflated view holder 
    Holder holder; 

    if (row == null) { 

     //Its the first time we are going to inflate the view, get new instance of Holder    
     holder = new Holder(); 

     //Inflate view 
     LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.message_list_item, parent, false); 

     //Put components in the holder 
     holder.countryName = (TextView) row.findViewById(R.id.comment); 
     holder.sharedSpecial = (ImageView) row.findViewById(R.id.sharedSpecial); 
     holder.wrapper = (LinearLayout) row.findViewById(R.id.wrapper); 

     //Attach holder to the row 
     row.setTag(holder); 

    }else{ 

     //Row was inflated before get holder 
     holder = (Holder)row.getTag(); 
    } 



    OneComment comment = getItem(position); 
    countryName.setText(comment.comment); 

    // Initiating Volley 
    final RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue(); 

    // Check if message has campaign or campaign/location attached 
    if (comment.campaign_id.equals("0") && comment.location_id.equals("0")) { 

     sharedSpecial.setImageBitmap(null); 

    } else if (!comment.campaign_id.equals("0") && !comment.location_id.equals("0")) { 

     // If both were shared 
     getSharedSpecialWithLocationURL = specialsLocationActionURL + comment.campaign_id + "/" + comment.location_id + JSON; 


     // GET JSON data and parse 
     JsonObjectRequest getCampaignLocationData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialWithLocationURL, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 

         // Parse the JSON: 
         try { 
          resultObject = response.getJSONObject("shared"); 

          imageObject = resultObject.getJSONObject("image"); 
          adImageURL = imageObject.getString("url"); 


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

         // Get and set image 
         Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(sharedSpecial); 
         sharedSpecial.setImageResource(0); 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.d("Error.Response", error.toString()); 
        } 
       } 
     ); 

     requestQueue.add(getCampaignLocationData); 


    } else if (!comment.campaign_id.equals("0") && comment.location_id.equals("0")) { 

     // Just the campaign is shared 
     getSharedSpecialURL = specialsActionURL + comment.campaign_id + JSON; 

     // Test Campaign id = 41 

     // GET JSON data and parse 

     JsonObjectRequest getCampaignData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialURL, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 

         // Parse the JSON: 
         try { 
          resultObject = response.getJSONObject("shared"); 

          imageObject = resultObject.getJSONObject("image"); 
          adImageURL = imageObject.getString("url"); 

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

         // Get and set image 
         Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(sharedSpecial); 
         sharedSpecial.setImageResource(0); 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.d("Error.Response", error.toString()); 
        } 
       } 
     ); 

     requestQueue.add(getCampaignData); 

     // Location set to empty 
    } 

    // If left is true, then yello, if not then set to green bubble 
    countryName.setBackgroundResource(comment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green); 
    wrapper.setGravity(comment.left ? Gravity.LEFT : Gravity.RIGHT); 

    return row; 
} 

private class Holder{ 
    TextView countryName; 
    ImageView sharedSpecial; 
    LinearLayout wrapper; 
} 

} 
+0

我稱通知數據集之前或之後,我添加新項目改變了嗎? – Sauron 2015-02-23 23:00:50

+0

這沒有奏效。圖像仍然會傳到下一條消息,或從另一條消息繼承圖像 – Sauron 2015-02-23 23:23:47

+0

我得到了這個圖像,但即使使用了notifyDataSetCahnge(),前面的圖像仍會填充那些空的ImageViews,我該如何解決這個問題? – Sauron 2015-02-24 03:35:33