2013-01-07 187 views
1

我想使用擴展BaseAdapter的CustomAdapter創建ListView。我有CustomAdapter(Context context, List<Map<String, String>> dataList)Android:使用CustomAdapter填充ListView

如何從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

post CustomAdapter代碼 –

+0

我編輯了我的問題。有我的來源。 – Martin

+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; 
} 
+0

謝謝四位幫忙。這非常有用。你真的幫助了我。 – Martin

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