因为我做一个类围脖的系统,想在listview做图片和文字,但有些只有图片或只有文字,应该怎么做? private ArrayList<String> commentNickname = null;
private ArrayList<String> sightComment = null;
private ArrayList<Bitmap> commentImage = null;ArrayList<HashMap<String, Object>> commentList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map= null;
while(i<commentCount){
map= new HashMap<String, Object>();
map.put("picture", commentImage.get(i));
map.put("commentnickname",commentNickname.get(i));
map.put("sightComment", sightComment.get(i));
commentList.add(map);
i++;
}
SightAdapter listAdapter = new SightAdapter(this, commentList, R.layout.sightcommentlistview,
new String[] { "commentnickname", "sightComment","picture"},
new int[] { R.id.LVuserNameCO,R.id.LVsightComment,R.id.LVimageView});
setListAdapter(listAdapter);几个arraylist都是从别地传过来的,里面可能有空,我需要一一对应,就是空的话就不显示,应该怎么办,不然会报错?
private ArrayList<String> sightComment = null;
private ArrayList<Bitmap> commentImage = null;ArrayList<HashMap<String, Object>> commentList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map= null;
while(i<commentCount){
map= new HashMap<String, Object>();
map.put("picture", commentImage.get(i));
map.put("commentnickname",commentNickname.get(i));
map.put("sightComment", sightComment.get(i));
commentList.add(map);
i++;
}
SightAdapter listAdapter = new SightAdapter(this, commentList, R.layout.sightcommentlistview,
new String[] { "commentnickname", "sightComment","picture"},
new int[] { R.id.LVuserNameCO,R.id.LVsightComment,R.id.LVimageView});
setListAdapter(listAdapter);几个arraylist都是从别地传过来的,里面可能有空,我需要一一对应,就是空的话就不显示,应该怎么办,不然会报错?
new String[] { "commentnickname", "sightComment","picture"}, new int[] { R.id.LVuserNameCO,R.id.LVsightComment,R.id.LVimageView});
红线处,会取到map中相应的值么?看看吧
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class myAdapter extends SimpleAdapter {
private int[] mTo;
private String[] mFrom;
private ViewBinder mViewBinder;
private List<? extends Map<String, ?>> mData;
private int mResource;
private int mDropDownResource;
private LayoutInflater mInflater;
public myAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/**
* @see android.widget.Adapter#getView(int, View, ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mResource);
}
private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View v;
if (convertView == null) {
v = mInflater.inflate(resource, parent, false);
final int[] to = mTo;
final int count = to.length;
final View[] holder = new View[count];
for (int i = 0; i < count; i++) {
holder[i] = v.findViewById(to[i]); }
v.setTag(holder);
// v.setTag(null);
} else {
v = convertView;
}
bindView(position, v);
return v;
}
private void bindView(int position, View view) {
final Map dataSet = mData.get(position);
if (dataSet == null) {
return;
}
final ViewBinder binder = mViewBinder;
final View[] holder = (View[]) view.getTag();
final String[] from = mFrom;
final int[] to = mTo;
final int count = to.length;
View convertView = mInflater.inflate(R.layout.sightcommentlistview, null); for (int i = 0; i < count; i++) {
final View v = holder[i];
if (v != null) {
final Object data = dataSet.get(from[i]);
// if(data i)
String text = data == null ? "" : data.toString();
if (text == null) {
text = "";
} boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, data, text);
}
if (!bound) {
if (v instanceof Checkable) {
if (data instanceof Boolean) {
((Checkable) v).setChecked((Boolean) data);
} else {
throw new IllegalStateException(v.getClass().getName() +
" should be bound to a Boolean, not a " + data.getClass());
}
} else if (v instanceof TextView) {
setViewText((TextView) v, text);
} else if (v instanceof ImageView) {
if (data instanceof Integer) {
setViewImage((ImageView) v, (Integer) data);
} else if(data instanceof Bitmap) {
if(data==null){
System.out.println("nopicture2");
}
setViewImage((ImageView) v, (Bitmap)data);
}
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleAdapter");
}
}
}
}
}
public void setViewImage(ImageView v, int value) {
v.setImageResource(value);
}
public void setViewImage(ImageView v, Bitmap bm) {
((ImageView) v).setImageBitmap(bm);
}
};能够实现显示图片和文字,但有空的地方,比如第3行有文字,但是没有图片,我在arratlist图片里面存了空的bitmap,然后用这个方法的话会在没图片的地方加上上一张图片,我不希望这样,能改成有不显示imageview吗?