Two-Column Menu Association for Android Small case

Source: Internet
Author: User

Because the project needs to do a simple menu association, in fact, it is simple to the left is a ListView selected time to refresh the right associated data, similar to the NetEase News Select Subscription page

It is important to note that when you click on the left menu list, the right-hand refresh will not necessarily come out of the data, this time will be refreshed into the adapter inside processing can be

Nonsense do not say direct code, the code contains detailed remarks

Total data Source Select color left and right list adapter list<twolistdata> Twolistdatas = new arraylist<twolistdata> ();p rivate colorstatelist Red;private colorstatelist black;private myadapter leftmyadapter;private myadapter rightMyAdapter;//left and right associative data list <String> leftstring = new arraylist<string> (); list<string> rightstring = new arraylist<string> ();//Right list private ListView lv_right; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_two_list); Resources resource = (resources) getresources (); red = (colorstatelist) resource.getcolorstatelist (r.color.color_ FF0000); black = (colorstatelist) resource.getcolorstatelist (r.color.color_000000); ListView Lv_left = (ListView) This.findviewbyid (R.id.lv_regison); lv_right = (ListView) This.findviewbyid (r.id.lv_ Block), for (int i = 0; i <, i++) {Twolistdatas.add (new Twolistdata ("left" + i)); leftstring = Getleftdata (); rightstring = TwolisTdatas.get (0). Getrightdatalist () Leftmyadapter = new Myadapter (Getleftdata ()); rightmyadapter = new Myadapter ( rightstring); Lv_left.setadapter (Leftmyadapter); Lv_right.setadapter (rightmyadapter);//The list on the left is selected to refresh the right list associated data in a timely manner lv_ Left.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2,long arg3) {leftmyadapter.setselected (arg2); rightstring = Twolistdatas.get (arg2). getrightdatalist (); Rightmyadapter.setmydata (rightstring);}); /Right list check display corresponding associated data lv_right.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick ( Adapterview<?> arg0, View arg1, int arg2,long arg3) {rightmyadapter.setselected (arg2); Toast.maketext (Twolistactivity.this, "select the" + arg2+ "location to the right of the left" + leftmyadapter.selectedpos + "location", Toast.length_long). Show ();}}); /** * Get Left data * * @return */public list<string> getleftdata () {list<string> leftdatalist = new Arraylist<st Ring> (); for (Twolistdata mtwolistdata:twolistDatas) {Leftdatalist.add (Mtwolistdata.getname ());} return leftdatalist;} /** * Adapter * * @author ZXC * */public class Myadapter extends Baseadapter {int selectedpos = 0;//identity Check public List<stri Ng> Getmydata () {return mydata;} public void Setmydata (list<string> mydata) {this.mydata = Mydata;notifydatasetchanged ();} Private list<string> mydata;public Myadapter (list<string> mydata) {this.mydata = MyData;} @Overridepublic int GetCount () {return mydata.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn mydata.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;}  @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder;if (Convertview = = null) {holder = new Viewholder (); Convertview = Layoutinflater.from (twolistactivity.this). Inflate (R.layout.adp_two_list, NULL); Holder.iv_title = (TextView) Convertview.findviewbyid (r.id.adptv_title); cOnvertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} Holder.iv_title.setText (Mydata.get (position)); if (Selectedpos = = position) {Holder.iv_title.setTextColor (red);} else {holder.iv_title.setTextColor (black);} return Convertview;} Private class Viewholder {private TextView iv_title;} /** * Logo selected and refreshed * * @param po */public void setselected (int po) {selectedpos = Po;notifydatasetchanged ();}}

The data model is as follows

public class Twolistdata {private String name;//callout private list<string> rightdatalist;public Twolistdata (String  Name) {this.name = Name;setrightdata (name, New Random (). Nextint (15) + 1);//random 1 to 15}/** * Add right associative data according to label *  * @param name * @param num */public void Setrightdata (String name, int num) {list<string> rightdatalist = new arraylist<string > (); for (int i = 0; i < num; i++) {Rightdatalist.add (name + i);} Setrightdatalist (rightdatalist);} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public list<string> getrightdatalist () {return rightdatalist;} public void Setrightdatalist (list<string> rightdatalist) {this.rightdatalist = Rightdatalist;}}

  

Effect

Two-Column Menu Association for Android Small case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.