- Separate your list item and the entire list in xml
- Create a custom adapter for your list items and create a list
- Depending on the criteria that you want to change, the color of the separator for each new item that 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);
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);
Slartibartfast
source share