我有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;
}
}
我稱通知數據集之前或之後,我添加新項目改變了嗎? – Sauron 2015-02-23 23:00:50
這沒有奏效。圖像仍然會傳到下一條消息,或從另一條消息繼承圖像 – Sauron 2015-02-23 23:23:47
我得到了這個圖像,但即使使用了notifyDataSetCahnge(),前面的圖像仍會填充那些空的ImageViews,我該如何解決這個問題? – Sauron 2015-02-24 03:35:33