- Seperate your list item and your whole list in xml
- Create a custom adapter for your list items and instantiate your
list
- Depending on the criteria you want change the divider colour for
each new item you add from the adapter to the list
Example:
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/sometext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<View
android:id="@+id/customdivider"
android:layout_width="fill_parent"
android:layout_height="1dp"
/>
</LinearLayout>
list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/totallynewlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public SubjectListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.listitem, null);
//Find the divider here and depending on your criteria change the colour - if required take data from main activity if you need it to change the colour
View divider= (View) vi.findViewById(R.id.customdivider);
if(your criteria)
divider.setBackgroundColor(R.color.white); //assuming white is defined in colors
else
set to whatever color
return vi;
}
}
In Your Activity
ListView list = (ListView) findViewById(R.id.totallynewlist);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
<//Pass whatever condition you want for your criteria here if you need to - optional>
ListPass.add(map);
adapter = new CustomAdapter(this, ListPass);
list.setAdapter(adapter);