If all you need is to set the color of one of the rows in the ListView
then you can do this:
In your ListView Layout "listview_item" give the root layout an id. Eg. "rlMainLayout"
In your getView
method you need to get a reference to the layout object. If it is a RelativeLayout then it would look something like this:
final RelativeLayout rlMainLayout = (RelativeLayout) convertView.findViewById(R.id.rlMainLayout);
Now you can set the color to your RelativeLayout
object like this:
rlMainLayout.setBackgroundColor(Color.RED);
----------------------------------------------------------
-- EDIT --
A STEP-BY-STEP EXAMPLE
(Please be advised that I did this in a Text Editor program .. so there could be some typos. )
You will need to alter your custom adapter class. Important: You will want to use a model class (in this case called MyListData). It will help when you need to maintain your software to separate your model from your views.
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;
}
}
Now you need a model class to hold the data. Let's call it MyListData (But you call it what you like)
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; }
}
Your custom ListView
layout could look something like the following. Please notice the root layout has an identifier 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>
In your Activity (eg. in the onCreate()
method) where the ListView
you will need to populate the data for the ListView
. This should not be done on the UI Thread
// 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);
Also in the activity where the ListView
is to be maintained set up some onClick
event handlers if you need them:
(I find that one of the small advantages of the ListView
is that the onClick
event is easier it implement than in the 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();
}
});