1

在我的应用程序中,用户订购了一个家用产品,应用程序在列表视图中显示产品名称和价格,我想为缺货产品设置不同的背景。谁能帮我。

公共类产品扩展 BaseAdapter {

public ArrayList<HashMap<String, String>> list;
Activity activity;

boolean Connection;

public Products(Activity activity,
        ArrayList<HashMap<String, String>> list) {
    super();
    this.activity = activity;
    this.list = list;
}

public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

class ViewHolder {

    TextView item_name,item_price,product_id,item_stock;


}

public View getView(final int position, View convertView,
        final ViewGroup parent) {
    // TODO Auto-generated method stub

    final ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();


    if (convertView == null) {

        convertView = inflater.inflate(R.layout.submenutext, null);

        if(ITEM_AVALIABLE_QUANTITY.equalsIgnoreCase("0")){

            convertView.setBackgroundResource(R.drawable.heading);
        }
        /*else{
            convertView.setBackgroundResource(R.drawable.sub_menu);
        }*/
        holder = new ViewHolder();

        holder.product_id = (TextView) convertView.findViewById(R.id.hide_text);
        holder.item_name = (TextView) convertView.findViewById(R.id.text);
        holder.item_price = (TextView) convertView.findViewById(R.id.price);
        //holder.item_stock = (TextView)convertView.findViewById(R.id.stock);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final HashMap<String, String> map = list.get(position);
    holder.product_id.setText(map.get(PRODUCT_ID));
    holder.item_name.setText(map.get(ITEM_NAME_COLUMN));
    holder.item_price.setText(map.get(ITEM_PRICE_COLUMN));
    holder.item_stock.setText(map.get(ITEM_AVALIABLE_QUANTITY));

    /*if(holder.item_stock.equals("0")){

        convertView.setBackgroundResource(R.drawable.heading);
    }*/

    return convertView;
}

}

4

3 回答 3

2

您可以将数据发送到具有自己自定义布局的自定义适配器。

主.java

  ListAdapter  adapter = new ListAdapter(this, arrayList);//ListAdapter is custom Adapter  
hotelList.setAdapter(adapter);  // hotellist is instance of ListView

这是您的自定义适配器 .java 文件。这个类有一个单独的视图,即list_row。

public class LazyAdapterForReminder extends BaseAdapter {    
private Activity activity;   
private static LayoutInflater inflater=null;   

public LazyAdapterForReminder(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);
 TextView lv = (TextView)vi.findViewByID(R.id.blabla);    

   if(stock==o)
       lv.setBackgroundResource(R.color.red);
   else
       lv.setBackgroundResource(R.color.green);

    return vi;
}

}

于 2013-03-01T10:44:31.030 回答
1

我希望您熟悉编写自定义适配器以在列表视图上进行设置。您可以使用产品名称和日期的缺货产品状态管理您的列表项目。在 CustomAdapter getView 方法上,您可以检查状态并设置列表项颜色的背景,或者设置相应的可绘制图像。例如:

   @Override        
     public View getView(int position, View convertView, ViewGroup parent) { 


                               if(stock==0){
                                        convertView.setBackgroundColor(R.color.grey);
                                 }else{
                                        convertView.setBackgroundColor(R.color.red);
                                   }

                          return convertView;
                    }
于 2013-03-01T10:26:18.123 回答
0

您可以在 Adapter getView 中设置背景。代码如下所示:

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder = null;

          if (convertView == null) {


            LayoutInflater vi = (LayoutInflater)  getContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listsearchitem, null);
            }

  //.................

            // then you could set the backgorun for the out of stock products
            convertView.setBackgroundResource(resid)

            //.................
      }
于 2013-03-01T10:22:02.210 回答