我ListView
接受ArrayAdapter
。当共享图像时,ArrayAdapter
有一个由{Picasso(来自Square的图书馆)填充的ImageView
。但是,在共享带有图像的消息并将其放置在该列表项的ImageView
中之后,如果共享了另一条没有图像的消息,则会将相同的先前图像填充到ImageView
中,而不是是空白的,因为它应该是没有共享图像。
我已经研究并尝试将ImageView
设置为null或透明,如果没有共享图像但没有任何效果。到目前为止,我尝试过:
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 :(得分:0)
您的getView方法存在问题。您没有将膨胀的xml附加到行。您正在全局声明组件(textview等),而最好创建一个可以附加的持有者类。
请为您的适配器尝试以下操作:
<强>更新强> 添加了构造函数,将列表作为参数。现在你像这样调用适配器:
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;
}
}