1

我想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;
}

}

4

2 回答 2

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;
}
于 2013-01-07T16:09:34.587 回答
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;
}
于 2013-01-07T16:15:17.703 回答