如何在项目(数量)小于3时更改ListView项目的backgroundColor

时间:2015-08-11 06:24:16

标签: java android xml

在我的MainActivity.java我希望以编程方式设置ListView项目的背景颜色,当数量小于3时,也请告诉我如何操作。谢谢提前。

这是我的自定义列表视图

single_product.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip"
    android:background="@drawable/list_selector">

<TextView
    android:id="@+id/Quantity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10000"
    android:textSize="24dip"
    android:textStyle="bold"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="24dp"
    android:layout_marginRight="51dp">
</TextView>

<TextView
    android:id="@+id/Unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Gal."
    android:layout_alignParentRight="true"
    android:layout_marginTop="26dp"
    android:textSize="20dp"
    android:layout_marginRight="15dp"/>

<TextView
    android:id="@+id/Description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="White"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="20dip"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/Code"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Wpu-01"
    android:layout_marginTop="2dp"
    android:layout_below="@+id/Description"
    android:layout_marginLeft="10dp"/>

<TextView
    android:id="@+id/Brand"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Code"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="2dp"
    android:text="Weber"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Urethane"
    android:layout_below="@+id/Code"
    android:layout_marginTop="2dp"
    android:layout_toRightOf="@+id/Brand"
    android:layout_marginLeft="5dp"
    android:id="@+id/Category" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Category"
    android:text="P"
    android:layout_marginLeft="12dp"
    android:paddingRight="5dp"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="2dp"
    android:textStyle="italic"
    android:id="@+id/P"/>

<TextView
    android:id="@+id/Price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10000.00"
    android:layout_below="@+id/Brand"
    android:layout_toRightOf="@+id/P"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="2dp" />

<TextView
    android:id="@+id/ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="11"
    android:layout_alignParentRight="true"
    android:visibility="invisible"/>
</RelativeLayout>

此处为主要活动代码

public void updateJSONdata() {

    orderlist = new ArrayList<HashMap<String, String>>();


    JSONObject json = jParser.getJSONFromUrl(PRODUCTLIST_URL);


    try {


        order = json.getJSONArray(GET_PRODUCT);

        for (int i = 0; i < order.length(); i++) {

            JSONObject c = order.getJSONObject(i);

            String id = c.getString(GET_ID);

            String brand = c.getString(GET_BRAND);

            String category = c.getString(GET_CATEGORY);

            String description = c.getString(GET_DESCRIPTION);

            String code = c.getString(GET_CODE);

            String quantity = c.getString(GET_QUANTITY);

            String unit = c.getString(GET_UNIT);

            String unitprice = c.getString(GET_UNITPRICE);

            HashMap<String, String> map = new HashMap<String, String>();

            map.put(GET_ID,id);

            map.put(GET_BRAND, brand);

            map.put(GET_CATEGORY, category);

            map.put(GET_DESCRIPTION, description);

            map.put(GET_CODE, code);

            map.put(GET_QUANTITY, quantity);

            map.put(GET_UNIT, unit);

            map.put(GET_UNITPRICE, unitprice);


            orderlist.add(map);

        }

    } catch (JSONException e) {

        e.printStackTrace();

    }

}

private void updateList() {

    lv = (ListView) findViewById(R.id.listview1);

    smpadater = new SimpleAdapter(this, orderlist,

            R.layout.single_product, new String[] { GET_ID, GET_BRAND, GET_CATEGORY,

            GET_DESCRIPTION, GET_CODE, GET_QUANTITY, GET_UNIT, GET_UNITPRICE}, new int[]{R.id.ID, R.id.Brand, R.id.Category,

            R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price });

    lv.setAdapter(smpadater);

    smpadater.notifyDataSetChanged();

}

final  public class LoadProducts extends AsyncTask<Void, Void, Boolean> {

    @Override

    protected void onPreExecute() {

        super.onPreExecute();

        pDialog = new ProgressDialog(MainActivity.this);

        pDialog.setMessage("Loading Order...");

        pDialog.setIndeterminate(false);

        pDialog.setCancelable(true);

        pDialog.show();

    }

    @Override

    protected Boolean doInBackground(Void... arg0) {

        updateJSONdata();

        return null;

    }

    @Override

    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        pDialog.dismiss();


        updateList();

    }

}

2 个答案:

答案 0 :(得分:1)

您必须从apdater更改getView方法的颜色:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.listitem, parent, false);
    }

    if (your condition) {
        view.setBackgroundResource(R.color.color1);
    } else {
        view.setBackgroundResource(R.color.color2);
    }



    return view;
}

答案 1 :(得分:1)

下面是CustomeAdapter,它将显示您的listview项目。

public class CustomAdapter extends BaseAdapter {

private final LayoutInflater inflater;
private List<MyObjectClass> list;

public CustomAdapter(Activity activity, List<MyObjectClass> list) {
    this.list = list;
    inflater = LayoutInflater.from(activity);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public MyObjectClass getItem(int position) {
    return list.get(position);
}

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

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

    ViewHolder viewHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.listview_item, null);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    MyObjectClass object = list.get(position);
    viewHolder.updateData(object);

    return convertView;
}

private class ViewHolder {

    private TextView tvQuantity;
    private TextView tvID;
    private View convertView;
    private TextView tvCode, tvPrice, tvBrand, tvCategory, tvDescription;


    public ViewHolder(View convertView) {
        this.convertView = convertView;
        tvQuantity = (TextView) convertView.findViewById(R.id.Quantity);
        tvID = (TextView) convertView.findViewById(R.id.ID);
        tvBrand = (TextView) convertView.findViewById(R.id.Brand);
        tvCategory = (TextView) convertView.findViewById(R.id.Category);
        tvDescription = (TextView) convertView.findViewById(R.id.Description);
        tvCode = (TextView) convertView.findViewById(R.id.Code);
        tvPrice = (TextView) convertView.findViewById(R.id.Price);
    }

    public void updateData(MyObjectClass object) {
        int quantity = object.getQuantity();
        if (quantity < 3) {
            convertView.setBackgroundColor(Color.BLUE);
        } else {
            //Here your color value should same as default color of listView
            convertView.setBackgroundColor(Color.WHITE);
        }
        tvQuantity.setText(String.valueOf(quantity) + "Gal");
        tvID.setText(object.getDescription());
        tvBrand.setText(object.getBrand());
        tvCategory.setText(object.getCategory());
        tvDescription.setText(object.getDescription());
        tvCode.setText(object.getCode());
        tvPrice.setText(object.getPrice());
    }
  }
}

从您的活动类中,您可以将适配器设置为listView。

这样的事情:

您可以添加包含所有这些详细信息的任意数量的项目。仅供参考,我刚添加了三个具有相同细节的项目。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<MyObjectClass> list = new ArrayList<MyObjectClass>();
    for(int i=0; i < 3; i++){
        list.add(new MyObjectClass(QUNTITY,GET_ID,GET_BRAND,GET_CATEGORY,GET_DESCRIPTION,GET_CODE,GET_PRICE));
    }
    ListView lv = (ListView) findViewById(R.id.listView);
    CustomAdapter adapter = new CustomAdapter(this,list);
    lv.setAdapter(adapter);

 }

您的对象类应该包含这些字段。

下面的

是您要求的对象类。

public class MyObjectClass {
private int quantity;

private String id;
private String brand;
private String category;
private String description;
private String code;
private String price;

public MyObjectClass(int quantity, String id, String brand, String category, String description, String code, String price) {
    this.quantity = quantity;
    this.id = id;
    this.brand = brand;
    this.category = category;
    this.description = description;
    this.code = code;
    this.price = price;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getBrand() {
    return brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}
}