如何根据变量值为每个项目列表视图设置背景色



我有一个非常简单的问题,但谷歌没有给我一个正确的答案。我需要根据变量值为listview中的每个项目设置背景颜色。handle状态项需要是一种颜色,而done必须是另一种颜色。

列表视图适配器

class ListViewAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
String[] time;
String[] clientName;
String[] district;
String[] address;
String[] goods;
String[] price;
String[] status;
public ListViewAdapter(Context context, String[] time, String[] clientName, String[] district, String[] address, String[] goods, String[] price, String[] status) {
this.context = context;
this.time = time;
this.clientName = clientName;
this.district = district;
this.address = address;
this.goods = goods;
this.price = price;
this.status = status;
}

@Override
public int getCount() {
return clientName.length; // было 0
}
@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView txtclientName;
TextView txttime;
TextView txtdistrict;
TextView txtaddress;
TextView txtgoods;
TextView txtprice;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Locate the TextViews in listview_item.xml
txtclientName = (TextView) itemView.findViewById(R.id.clientNameSingle);
txttime = (TextView)itemView.findViewById(R.id.timeSingle);
txtdistrict = (TextView)itemView.findViewById(R.id.districtSingle);
txtaddress = (TextView) itemView.findViewById(R.id.addressSingle);
txtgoods = (TextView)itemView.findViewById(R.id.goodsSingle);
txtprice = (TextView)itemView.findViewById(R.id.priceSingle);
// Capture position and set to the TextViews
txtclientName.setText(clientName[position]);
txttime.setText(time[position]);
txtdistrict.setText(district[position]);
txtaddress.setText(address[position]);
txtgoods.setText(goods[position]);
txtprice.setText(price[position]);
itemView.setBackgroundColor(Color.parseColor("#00FF7F"));
return itemView;
}
}

订单列表类

public class OrderList extends Activity {
ListView list;
ListViewAdapter adapter;
String[] time;
String[] clientName;
String[] district;
String[] address;
String[] goods;
String[] price;
String[] status;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
time = new String[]{"09:00", "09:00"};
clientName = new String[]{"Анна", "Ольга"};
district = new String[]{"Калининский", "Калининский"};
address = new String[]{"Морская набережная д35", "Проспект Непокоренных 49"};
goods = new String[]{"Елка 1.8м", "Сосна 2м"};
price = new String[]{"1499", "2299"};
status = new String[]{"done", "handle"};
list = (ListView) findViewById(R.id.listview);
// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(this, time, clientName, district, address, goods, price, status);
// Binds the Adapter to the ListView
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Animation animation1 = new AlphaAnimation(0.3f, 1.0f);
animation1.setDuration(2000);
view.startAnimation(animation1);

Intent intent = new Intent(OrderList.this, SingleItemView.class);
intent.putExtra("time", time);
intent.putExtra("clientName", clientName);
intent.putExtra("district", district);
intent.putExtra("address", address);
intent.putExtra("goods", goods);
intent.putExtra("price", price);
intent.putExtra("position", position);
intent.putExtra("status", status);
startActivity(intent);
}
});
}
}

如果您只需要设置ListView中某一行的颜色,那么您可以执行以下操作:

在您的ListView布局"ListView_item"中,给根布局一个id。例如"rlMainLayout">

getView方法中,您需要获得对布局对象的引用。如果它是RelativeLayout,那么它看起来像这样:

final RelativeLayout rlMainLayout = (RelativeLayout) convertView.findViewById(R.id.rlMainLayout);

现在,您可以将颜色设置为RelativeLayout对象,如下所示:

rlMainLayout.setBackgroundColor(Color.RED);

-----------------------------------------------------------

--编辑--

分步示例

(请注意,我是在文本编辑器程序中这样做的。因此可能会有一些拼写错误。)

您将需要更改您的自定义适配器类。重要提示:您将希望使用一个模型类(在本例中称为MyListData)。当您需要维护软件以将模型与视图分离时,它将有所帮助。

public class MyListDataAdapter extends ArrayAdapter<MyListData> {
private static final String TAG = "MyListDataAdapter";

public MyListDataAdapter(Context context, ArrayList<MyListData> data) {
super(context, 0, data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
final MyListData data = getItem(position);
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_item, parent, false);
}
TextView txtclientName;
TextView txttime;
TextView txtdistrict;
TextView txtaddress;
TextView txtgoods;
TextView txtprice;
// Locate the TextViews in listview_item.xml
txtclientName = (TextView) itemView.findViewById(R.id.clientNameSingle);
txttime = (TextView)itemView.findViewById(R.id.timeSingle);
txtdistrict = (TextView)itemView.findViewById(R.id.districtSingle);
txtaddress = (TextView) itemView.findViewById(R.id.addressSingle);
txtgoods = (TextView)itemView.findViewById(R.id.goodsSingle);
txtprice = (TextView)itemView.findViewById(R.id.priceSingle);
RelativeLayout rlMainLayout = (RelativeLayout) convertView.findViewById(R.id.rlMainLayout);
Color originalColorValue = tvNumberOfItems.getDrawingCacheBackgroundColor();
// Capture position and set to the TextViews
txtclientName.setText(data.getClientName());
txttime.setText(data.getTime());
txtdistrict.setText(data.getDistrict());
txtaddress.setText(data.getAddress());
txtgoods.setText(data.getGoods());
txtprice.setText(data.getPrice());
if(data.getIsSelected){
rlMainLayout.setBackgroundColor(Color.RED);
}
else{
rlMainLayout.setBackgroundColor(originalColorValue);
}
return itemView;
}
}

现在您需要一个模型类来保存数据。让我们称之为MyListData(但你喜欢怎么称呼它就怎么称呼它)

public class MyListData {
// Add more as you need
// I have used the ones you have defined...
private String time;
private String clientName;
private String district;
private String address;
private String goods;
//... but I think price should be of type double so that you can do calculations if needed
private String price;
private String status;
private boolean isSelected = false;
public MyListData(){
}

public void setTime(String time){ this.time = time; }
public void setClientName(String clientName){ this.clientName = clientName; }
public void setDistrict(String district){ this.district = district; }
public void setAddress(String address){ this.address = address; }
// TODO: !! ..Please add the others goods, price, status ..
public void setIsSelected(boolean isSelected){ this.isSelected = isSelected; }
// Now add your public getters!!
public String getTime(){ return this.time; }
public String getClientName(){ return this.setClientName; }
// TODO: !! ... Please add the others for goods, price, status...
public boolean getIsSelected(){ return this.isSelected; }
}

您的自定义ListView布局可能如下所示。请注意,根布局有一个标识符rlMainLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rlMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
>
<TextView
android:id="@+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
>
<TextView
android:id="@+id/tvAddress"
android:text="address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>

ListView所在的"活动"(例如,在onCreate()方法中)中,您需要填充ListView的数据。这应该而不是在UI线程上完成

// This will contain all the data you need from you model class in an ArrayList
ArrayList<MyListData> arrayListData = new ArrayList<MyListData>();
MyListDataAdapter adapter = new MyListDataAdapter(this, arrayListData);
for (MyListData g : result) {
adapter.add(g);
}
list.setAdapter(adapter);

此外,在要维护ListView的活动中,如果需要,请设置一些onClick事件处理程序:(我发现ListView的一个小优点是onClick事件比RecycleView事件更容易实现)

list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
MyListData data = (MyListData) parent.getItemAtPosition(position);
data.setIsSelected = true;
// Update the listview with the changes to your MyListData
adapter.notifyDataSetChanged();
}
});

希望您在listview_item布局文件中使用Layout在适配器文件中初始化后,您可以替换布局的背景色

int[] color = {R.color.colorPrimary,R.color.colorAccent,R.color.colorPrimaryDark};
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.listview_item);
relativeLayout.setBackgroundColor(color[2]);

最新更新