我想使用扩展ListView
的CustomAdapter创建BaseAdapter
。我有CustomAdapter(Context context, List<Map<String, String>> dataList)
。
如何从Map
获取数据并在getView()
方法中设置我的观点?在我尝试使用SimpleAdapter
之前,我需要设置存储在SD上的位图,并将图像路径存储在地图中。感谢每一位帮助。
这是CustomAdapter的源代码:
public class CustomAdapter extends BaseAdapter {
private Context context;
private String pricePrecision = null;
private List<Map<String, String>> data;
public CustomAdapter(Context context, List<Map<String, String>> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return (data == null) ? 0 : data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
return null;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}
}
答案 0 :(得分:2)
将getView方法更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
viewHolder.itemThumbnail.setImageBitmap(data.get(position).get("thumbnail"));
viewHolder.itemName.setText(data.get(position).get("name"));
viewHolder.itemPrice.setText(""+data.get(position).get("price"));
return viewHolder;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}
答案 1 :(得分:0)
(ρяσѕρєяK)代码的一点点补充。 您可能希望在缩略图上设置侦听器。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
viewHolder.itemThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
viewHolder.itemThumbnail.setImageBitmap(data.get(position).get(
"thumbnail"));
viewHolder.itemName.setText(data.get(position).get("name"));
viewHolder.itemPrice.setText("" + data.get(position).get("price"));
return viewHolder;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}