List中的ImageView不会清空之前的图片
ImageView in List will not clear previous image
我有 ListView
接受 ArrayAdapter
。 ArrayAdapter
有一个 ImageView
,在共享图像时由 Picasso(来自 Square 的库)填充。但是,在共享带有图像的消息并将其放置在该列表项的 ImageView
中之后,如果共享了另一条没有图像的消息,则先前的相同图像将填充到 ImageView
中,而不是是空白的,如果没有共享图像,它应该是空白的。
我已经研究并尝试将 ImageView
设置为空或透明,如果没有共享图像但没有任何效果。到目前为止我已经尝试过:
sharedSpecial.setImageBitmap(null);
和 sharedSpecial.setImageResource(0);
但都不起作用。
如果没有共享当前图像,我如何"clear" ImageView
显示 ListView
中的上一张图像?
ArrayAdapter
class 设置分享类型的图片如下:
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;
}
}
您的 getView 方法有问题。您没有将膨胀的 xml 附加到该行。您正在全局声明组件(textview 等),同时最好创建一个可以附加的持有人 class。
能否为您的适配器尝试以下操作:
更新
添加了将列表作为参数的构造函数。现在你像这样调用适配器:
DiscussArrayAdapter adapter = new DiscussArrayAdapter(context, R.layout.message_list_item, countries);
listView.setAdapter(adapter);
那么信任适配器 notifyDataSetChanged 很重要。
在 activity 中全局创建国家/地区列表和适配器,每当您从列表中添加/删除项目时,只需调用 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;
}
}
我有 ListView
接受 ArrayAdapter
。 ArrayAdapter
有一个 ImageView
,在共享图像时由 Picasso(来自 Square 的库)填充。但是,在共享带有图像的消息并将其放置在该列表项的 ImageView
中之后,如果共享了另一条没有图像的消息,则先前的相同图像将填充到 ImageView
中,而不是是空白的,如果没有共享图像,它应该是空白的。
我已经研究并尝试将 ImageView
设置为空或透明,如果没有共享图像但没有任何效果。到目前为止我已经尝试过:
sharedSpecial.setImageBitmap(null);
和 sharedSpecial.setImageResource(0);
但都不起作用。
如果没有共享当前图像,我如何"clear" ImageView
显示 ListView
中的上一张图像?
ArrayAdapter
class 设置分享类型的图片如下:
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;
}
}
您的 getView 方法有问题。您没有将膨胀的 xml 附加到该行。您正在全局声明组件(textview 等),同时最好创建一个可以附加的持有人 class。
能否为您的适配器尝试以下操作:
更新 添加了将列表作为参数的构造函数。现在你像这样调用适配器:
DiscussArrayAdapter adapter = new DiscussArrayAdapter(context, R.layout.message_list_item, countries);
listView.setAdapter(adapter);
那么信任适配器 notifyDataSetChanged 很重要。
在 activity 中全局创建国家/地区列表和适配器,每当您从列表中添加/删除项目时,只需调用 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;
}
}