Android Listview Select Item -


i have minimum ten item food listview.

i want select 1 or more item food .

and pressed order button.

then show me total price select item food.

how possible please tell me?

menulistitem:

package com.example.project_hotel_management;  import java.util.arraylist; import java.util.list;  import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listview; import android.widget.toast;  public class menulistitem extends activity{      listview lv;     myadapter adapter;      @override     protected void oncreate(bundle savedinstancestate) {         // todo auto-generated method stub         super.oncreate(savedinstancestate);     setcontentview(r.layout.menuitemlist_layout);      lv=(listview)findviewbyid(r.id.listview1);      adapter=new myadapter(this , getlistitem());     lv.setadapter(adapter);      lv.setonitemclicklistener(new onitemclicklistener() {          @override         public void onitemclick(adapterview<?> arg0, view arg1, int arg2,                 long arg3) {             // todo auto-generated method stub             toast.maketext(getapplicationcontext(), "d", toast.length_long).show();          }     });     }      public arraylist<listitem> getlistitem() {         arraylist<listitem> alllist=new arraylist<listitem>();          listitem l1=new listitem("beef_burguer", 1, r.drawable.beef_burguer);         listitem l2=new listitem("bacon_cheese_burger", 2, r.drawable.bacon_cheese_burger);         listitem l3=new listitem("frings", 3, r.drawable.frings);         listitem l4=new listitem("burger_kingse", 4, r.drawable.burger_kingse);         listitem l5=new listitem("mint_oreo", 5, r.drawable.mint_oreo);         listitem l6=new listitem("sourdough_bread", 6, r.drawable.sourdough_bread);         listitem l7=new listitem("sandwich", 7, r.drawable.sandwich);         listitem l8=new listitem("sandwich_loaded", 8, r.drawable.sandwich_loaded);         listitem l9=new listitem("mustard", 9, r.drawable.mustard);         listitem l10=new listitem("cheese_burger", 10, r.drawable.cheese_burger);          alllist.add(l1);         alllist.add(l2);         alllist.add(l3);         alllist.add(l4);         alllist.add(l5);         alllist.add(l6);         alllist.add(l7);         alllist.add(l8);         alllist.add(l9);         alllist.add(l10);            return alllist;     }      public void order(view v) {          string result="";         list<integer> resultlist=adapter.getcheckeditemposition();         (int = 0; < resultlist.size(); i++) {             result+=string.valueof(resultlist.get(i))+"\n";         }         toast.maketext(getapplicationcontext(), result, toast.length_long).show();      }  } 

myadapter:

package com.example.project_hotel_management;  import java.util.arraylist; import java.util.hashmap; import java.util.list;  import android.app.activity; import android.content.clipdata.item; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.imageview; import android.widget.textview; import android.widget.toast;  public class myadapter extends arrayadapter<listitem>{      activity context;     arraylist<listitem> listitem;      hashmap<integer, boolean> mychecked=new hashmap<integer, boolean>();      public myadapter(activity context, arraylist<listitem> listitem) {         super(context, r.layout.adapter_layout, listitem);          this.context=context;          this.listitem=listitem;         (int = 0; < listitem.size(); i++) {         mychecked.put(i, false);     }      }       public void togglechecked(int position) {         if(mychecked.get(position)){             mychecked.put(position, false);             toast.maketext(getcontext(), "false", toast.length_long).show();         }else{             mychecked.put(position, true);             toast.maketext(getcontext(), "false", toast.length_long).show();         }         notifydatasetchanged();     }      public list<integer> getcheckeditemposition() {         list<integer> checkeditemposition=new arraylist<integer>();          (int = 0; < mychecked.size(); i++) {             checkeditemposition.add(i);         }         return checkeditemposition;     }  //  public list<integer> getcheckeditem() { //      list<integer> checkeditem=new arraylist<integer>(); //       //      (int = 0; < mychecked.size(); i++) { //          checkeditem.add(); //      } //      return checkeditem; //  }     @override     public view getview(int position, view convertview, viewgroup parent) {         view view=convertview;              layoutinflater inflat=context.getlayoutinflater();             view=inflat.inflate(r.layout.adapter_layout, null, false);             textview txtname=(textview) view.findviewbyid(r.id.txtname);             textview txtprice=(textview) view.findviewbyid(r.id.txtprice);             imageview image=(imageview) view.findviewbyid(r.id.imageview1);              listitem l=listitem.get(position);             toast.maketext(getcontext(), string.valueof(listitem.get(position)), toast.length_long              ).show();              txtname.settext(l.getname());             txtprice.settext(string.valueof(l.getprice()));             image.setimageresource(l.getimage());              checkbox chekboxs=(checkbox) view.findviewbyid(r.id.checkbox1);              boolean chekbox=mychecked.get(position);              if(chekbox!=null){                 chekboxs.setchecked(chekbox);             }           return view;     } } 

listitem:

package com.example.project_hotel_management;  public class listitem {      string name;     integer price;     integer image;        public listitem(string name, integer price, integer image) {         super();         this.name = name;         this.price = price;         this.image = image;     }       public string getname() {         return name;     }     public void setname(string name) {         this.name = name;     }     public integer getprice() {         return price;     }     public void setprice(integer price) {         this.price = price;     }     public integer getimage() {         return image;     }     public void setimage(integer image) {         image = image;     }     @override     public string tostring() {         return "listitem [name=" + name + ", price=" + price + ", image="                 + image + "]";     } } 

i use it's not working:

lv.setonitemclicklistener(new onitemclicklistener() {  @override public void onitemclick(adapterview<?> arg0, view arg1, int position,         long arg3) {     // todo auto-generated method stub     listitem ls=getlistitem().get(position);     toast.maketext(getapplicationcontext(), string.valueof(ls.getprice()), toast.length_long).show();   }   }); 

you can set android:choicemode="multiplechoice" on listview. can implement selector define how selected item highlighted. example of selector:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">      <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>     <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>     <item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>  </selector> 

once 1 or more items in listview selected, can selected items by:

int len = listview.getcount(); sparsebooleanarray checked = listview.getcheckeditempositions(); (int = 0; < len; i++) if (checked.get(i)) { string item = cont_list.get(i); /* calculate price  */ } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -