Reputation: 5854
i'm trying to set different textcolor for different list items of a listview. How to achieve it? Any help is really appreciated and thanks in advance...
Upvotes: 0
Views: 160
Reputation: 670
Your Activity related to the List.xml should extend from ListActivity class
inside onCreate method:
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();//RowData should get you, your data
CustomAdapter adapter = new CustomAdapter(this, R.layout.list, R.id.title, data);
setListAdapter(adapter);
Add a new CustomAdapter class:
public class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
if(position%2==0)
convertView.setBackgroundColor(color.darker_gray);
}
}
Upvotes: 0
Reputation: 128448
For that you have to define a custom adapter by extending BaseAdapter. Now define a class and extends BaseAdapter, inside it you can perform the change color kind of stuffs inside the getView()
method.
For defining custom listview, here are examples for the same. Go through the examples and try to implement in your way.
Upvotes: 3