I am creating a custom dialog having two images as a title and a listview inside the dialog. in that listview there is some items i am showing and now i want that the items in the listview should get different background? like suppose i have 5 items to show in a list view then the items in position 1,3,5 are having different background than items at position 2,4. is it possible? help me
Asked
Active
Viewed 289 times
2 Answers
0
You can use a custom adapter when loading items in your listview.
Create a layout xml for an item in the listview and set it.
Like myitemlist_row.xml and in the activity :
MyAdapter _adapter = new MyAdapter(getActivity().getApplicationContext(), R.layout.myitemlist_row, null, uiBindFrom, uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER, this);
_mylist.setAdpater(_adapter);
Sample code for the adapter :
@Override
public void bindView(View view, Context context, Cursor cursor)
{
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder)view.getTag();
if (holder == null)
{
holder = new ViewHolder();
holder.Background_Layout = (LinearLayout) view.findViewById(R.id.Background_Layout);
}
//you can use now holder.Background_Layout.setBackgroundColor or setBackgroundDrawable accordind to the data, as you want
Hope it helps :-)

chrisendymion
- 175
- 1
- 9
0
Try this, assuming you're using a adapter...
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.<item_layout>, null);
}
else
if(pos%2==0)
vi.setBackgroundResource(<image_1_res_id>);
else
vi.setBackgroundResource(<image_2_res_id>);
return vi;
}

Rahmathullah M
- 2,676
- 2
- 27
- 44