java - Listview : How to remain highlighted/selected after press back button? -
how keep listview remain highlighted when pressed button? possible? i've went through link no replies.
i managed set background, image, , text in order change when user press list.
one of xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/light_grey" /> <item android:state_selected="true" android:drawable="@color/light_grey"/> <item android:state_activated="true" android:drawable="@color/light_grey"/> <item android:state_focused="false" android:drawable="@color/white" />
notification_row.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/activated_bg" > <imageview android:id="@+id/imageview1" android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center" android:layout_marginleft="5dp" android:padding="4dp" android:src="@drawable/checkbox" /> <linearlayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.73" android:orientation="vertical" > <textview android:id="@+id/id_row" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" /> <textview android:id="@+id/title_row" android:layout_width="match_parent" android:layout_height="25dp" android:layout_margintop="5dp" android:layout_marginleft="5dp" android:padding="2dp" android:text="title" android:textcolor="#000000" android:textsize="16sp" android:textstyle="bold" /> <textview android:id="@+id/content_row" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" /> <textview android:id="@+id/sendtime_row" android:layout_width="match_parent" android:layout_height="25dp" android:layout_marginleft="5dp" android:maxlength="100" android:paddingbottom="2dp" android:paddingleft="3dp" android:paddingright="3dp" android:text="date" android:textcolor="#808080" android:textsize="15sp" /> </linearlayout> </linearlayout>
in activity have listview put this:
create global variable: bundle savedinstancestate;
@override protected void onrestart() { // todo auto-generated method stub oncreate(savedinstancestate); super.onrestart(); }
in oncreat
e handle listview
or set adapter
listview
.
here simple example how this:
package com.example.testapp; import com.example.main.util.testactivity; import android.app.listactivity; import android.content.context; import android.content.intent; import android.graphics.color; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; public class mainactivity extends listactivity { private static final string[] countries = new string[] { "belgium", "france", "france_", "italy", "germany", "spain" }; private myarrayadapter adapter; bundle savedinstancestate; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); adapter = new myarrayadapter(mainactivity.this); getlistview().setadapter(adapter); } private class myarrayadapter extends baseadapter { private layoutinflater minflater; public myarrayadapter(context con) { // todo auto-generated constructor stub minflater = layoutinflater.from(con); } @override public int getcount() { // todo auto-generated method stub return countries.length; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub final listcontent holder; view v = convertview; if (v == null) { v = minflater.inflate(r.layout.my_spinner_style, null); holder = new listcontent(); holder.name = (textview) v.findviewbyid(r.id.textview1); v.settag(holder); } else { holder = (listcontent) v.gettag(); } holder.name.settext("" + countries[position]); holder.name.setonclicklistener(montitleclicklistener3); return v; } } static class listcontent { textview name; } public onclicklistener montitleclicklistener3 = new onclicklistener() { public void onclick(view v) { final int position = getlistview().getpositionforview( (view) v.getparent()); v.setbackgroundcolor(color.red); startactivity(new intent(mainactivity.this, testactivity.class)); // log.d("you click on ratings", "you click on ratings"); // v.setbackgroundcolor(color.white); } }; protected void onrestart() { // adapter.notifydatasetchanged(); oncreate(savedinstancestate); }; }
or can same style reverse. mean have deselect litview after go next activity. can in list item click or whatever have used..
Comments
Post a Comment