Java 当项目(数量)小于3时,如何更改ListView项目的背景色
在我的Java 当项目(数量)小于3时,如何更改ListView项目的背景色,java,android,xml,Java,Android,Xml,在我的MainActivity.java中,当数量少于3时,我想通过编程设置ListView项目的背景颜色,并且请告诉我如何设置。提前感谢 这是我的自定义列表视图 single_product.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_
MainActivity.java
中,当数量少于3时,我想通过编程设置ListView
项目的背景颜色,并且请告诉我如何设置。提前感谢
这是我的自定义列表视图
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();
public void updateJSONdata(){
orderlist=新的ArrayList();
JSONObject json=jParser.getJSONFromUrl(PRODUCTLIST\uURL);
试一试{
order=json.getJSONArray(GET_PRODUCT);
对于(int i=0;i
}
final公共类LoadProducts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity.this);
setMessage(“加载顺序…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的布尔doInBackground(无效…arg0){
updateJSONdata();
返回null;
}
@凌驾
受保护的void onPostExecute(布尔结果){
super.onPostExecute(结果);
pDialog.disclose();
updateList();
}
}
您必须从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;
}
下面的类是CustomeApter,它将显示您的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());
}
}
}
在适配器类中,可以检查数量是否小于或大于3的条件。如果条件为true,则可以更改列表视图项的颜色。能否提供如何更改的示例。谢谢将适配器代码发布在此处我在上面发布了我的代码请检查设置listview适配器后为什么调用notifyDataSetChanged()。这不是必需的。但Marta我不使用baseadapterOk抱歉,在您输入您的适配器代码之前,我回答了您的问题:\n但是,如果您想根据数量修改视图,您必须使用自定义适配器。如果你是android的新手,你可以从ArrayAdapter开始。遵循这篇精彩的教程,重点关注“使用自定义ArrayAdapter”部分,但是如果我不使用BaseAdapter,我只使用SimpleAdapter,那么我该如何做呢?为什么不使用BaseAdapter,对于这个要求,你必须使用BaseAdapter。哈哈哈,我是android应用程序开发的新手。我不知道如何将我的代码转换为BaseAdapter。你能教我如何为我的自定义listview创建自定义适配器吗?我想要这样的SMAdater=new SimpleAdapter(这个,orderlist,R.layout.single_product,new String[]{GET_ID,GET_BRAND,GET_CATEGORY,GET_DESCRIPTION,GET_CODE,GET_QUANTITY,GET_UNIT,GET_UNITPRICE},新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(SMAdapter);对于您的要求,我已经发布了我的代码,一旦使用了它。如果您有任何疑问,请告诉我。
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());
}
}
}
@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;
}
}