如何在android中设置listview特定行的背景?

时间:2013-03-01 10:15:04

标签: android android-listview

在我的应用程序中,用户订购家用产品,应用程序显示PRODUCT NAME&在列表视图中的价格,我想为缺货产品设置不同的背景。任何人都可以帮助我。

公共类产品扩展了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;
}

}

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

Main.java

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

这是您的自定义Adapter .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;
}

}

答案 2 :(得分:0)

您可以在适配器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)

            //.................
      }