我有一个非常简单的问题,但谷歌没有给我一个正确的答案。我需要为我的listview
中的每个项目设置背景颜色,并对变量值进行去除。 handle
状态项必须是一种颜色,而done
必须是另一种颜色。
Listview适配器
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);
}
});
}
}
答案 0 :(得分:0)
如果您只需要设置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>
在您的活动中(例如,在onCreate()
方法中)ListView
,您需要填充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();
}
});
答案 1 :(得分:0)
希望您在listview_item布局文件中使用布局 在适配器文件中初始化后,您可以替换布局的背景颜色
numpy