gpt4 book ai didi

Android:使用 CustomAdapter 填充 ListView

转载 作者:行者123 更新时间:2023-11-29 21:51:48 26 4
gpt4 key购买 nike

我想创建 ListView使用扩展 BaseAdapter 的 CustomAdapter .我有CustomAdapter(Context context, List<Map<String, String>> dataList) .

如何从 Map 获取数据并在 getView() 中设置我的 View 方法?在我尝试使用 SimpleAdapter 之前但我需要设置存储在 SD 上的位图,以及存储在 map 中的图像路径。感谢您的帮助。

这是我的 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;
}

最佳答案

将您的 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;
}

关于Android:使用 CustomAdapter 填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com