android - how to change color of list item when it is selected in listview? -
android - how to change color of list item when it is selected in listview? -
i have 1 main activity class in define string array fetch list xml file saved in res/value contain list of item . want when item selected color alter yellowish . how please help :)
thanks in advance ::)
my 2 file's list_data.xml , main activity.java
list_data.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="tias_list"> <item>about us</item> <item> offered</item> <item> year </item> <item>process</item> <item>item1</item>> <item>item2</item>> <item>item3</item>> <item>item4</item> </string-array> </resources>
my main activity class code is
public class mainactivity extends listactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); string[] tias_list = getresources().getstringarray(r.array.tias_list); this.setlistadapter(new arrayadapter<string>(this, r.layout.list_item, r.id.label,tias_list)); listview lv = getlistview(); lv.setcachecolorhint(0); lv.setbackgroundresource(r.drawable.black); lv.setonitemclicklistener(new onitemclicklistener(){ public void onitemclick(adapterview<?> parent,view view,int position, long id) { if (position == 0) { intent myintent = new intent(getapplicationcontext(), singlelistitem.class); startactivity(myintent); } else if(position == 1) { intent myintent = new intent(getapplicationcontext(), lastyear.class); startactivity(myintent); } else if(position == 2) { intent myintent = new intent(getapplicationcontext(), la.class); startactivity(myintent); } else if(position == 3) { intent myintent = new intent(getapplicationcontext(),la.class); startactivity(myintent); } else if(position == 4) { intent myintent = new intent(getapplicationcontext(), singlelistitem.class); startactivity(myintent); } else if(position == 5) { intent myintent = new intent(getapplicationcontext(), singlelistitem.class); startactivity(myintent); } else if(position == 6) { intent myintent = new intent(getapplicationcontext(), singlelistitem.class); startactivity(myintent); } else if(position == 7) { intent myintent = new intent(getapplicationcontext(), singlelistitem.class); startactivity(myintent); } } }); }
the adapter using default adapter. however, want modify it's properties. you'll need implement custom adapter - adapter of own. google custom listview adapter if don't know how that.
and in custom adapter, there'll getview() method , in getview() method of custom adapter check if position of item same selected item. if yes, set background colour.
here's finish reply : http://stackoverflow.com/a/16978159/1239966
android android-listview
Comments
Post a Comment