android - How to add startActivity into OnClickListener in Adapter? -


here adapter.

public class myadapter extends recyclerview.adapter<myadapter.viewholder> {  private static final int type_header = 0;  // declaring variable understand view being worked on // if viaew under inflation , population header or item private static final int type_item = 1;  private string mnavtitles[]; // string array store passed titles value mainactivity.java private int micons[];       // int array store passed icons resource value mainactivity.java  private string name;        //string resource header view name private int profile;        //int resource header view profile picture private string email;       //string resource header view email static context context;   // creating viewholder extends recyclerview view holder // viewholder used to store inflated views in order recycle them  public static class viewholder extends recyclerview.viewholder {     int holderid;      textview textview;     imageview imageview;     imageview profile;     textview name;     textview email;     context contxt;     intent intent;       public viewholder(view itemview,int viewtype,context c) {                 // creating viewholder constructor view , viewtype parameter         super(itemview);         contxt = c;         itemview.setclickable(true);         itemview.setonclicklistener(click);          // here set appropriate view in accordance the view type passed when holder object created          if(viewtype == type_item) {             textview = (textview) itemview.findviewbyid(r.id.rowtext); // creating textview object id of textview item_row.xml             imageview = (imageview) itemview.findviewbyid(r.id.rowicon);// creating imageview object id of imageview item_row.xml             holderid = 1;                                               // setting holder id 1 object being populated of type item row         }         else{               name = (textview) itemview.findviewbyid(r.id.name);         // creating text view object header.xml name             email = (textview) itemview.findviewbyid(r.id.email);       // creating text view object header.xml email             profile = (imageview) itemview.findviewbyid(r.id.circleview);// creating image view object header.xml profile pic             holderid = 0;                                                // setting holder id = 0 object being populated of type header view         }     }        private view.onclicklistener click=new view.onclicklistener(){         @override         public void onclick(view v) {             switch(getposition())             {                 case 1:                     intent = new intent(context, me.class);                     startactivity(intent);             }         }      }; }    myadapter(string titles[],int icons[],string name,string email, int profile,context passedcontext){ // myadapter constructor titles , icons parameter     // titles, icons, name, email, profile pic passed main activity     mnavtitles = titles;                //have seen earlier     micons = icons;     name = name;     email = email;     profile = profile;                     //here assign passed values values declared here     this.context = passedcontext;      //in adapter    }    //below first ovverride method oncreateviewholder called when viewholder //created, in method inflate item_row.xml layout if viewtype type_item or else inflate header.xml // if viewtype type_header // , pass view holder  @override public myadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) {      if (viewtype == type_item) {         view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.item_row,parent,false); //inflating layout          viewholder vhitem = new viewholder(v,viewtype,context); //creating viewholder , passing object of type view          return vhitem; // returning created object          //inflate layout , pass view holder      } else if (viewtype == type_header) {          view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.header,parent,false); //inflating layout          viewholder vhheader = new viewholder(v,viewtype,context); //creating viewholder , passing object of type view          return vhheader; //returning object created       }     return null;  }  //next override method called when item in row needed displayed, here int position // tells item @ position being constructed displayed , holder id of holder object tell // view type being created 1 item row @override public void onbindviewholder(myadapter.viewholder holder, int position) {     if(holder.holderid ==1) {                              // list view going called after header view decrement         // position 1 , pass holder while setting text , image         holder.textview.settext(mnavtitles[position - 1]); // setting text array of our titles         holder.imageview.setimageresource(micons[position -1]);// settimg image array of our icons     }     else{          holder.profile.setimageresource(profile);           // set resources header view         holder.name.settext(name);         holder.email.settext(email);     } }  // method returns number of items present in list @override public int getitemcount() {     return mnavtitles.length+1; // number of items in list +1 titles including header view. }   // witht following method check type of view being passed @override public int getitemviewtype(int position) {     if (ispositionheader(position))         return type_header;      return type_item; }  private boolean ispositionheader(int position) {     return position == 0; } 

}

here onclicklistener within

    private view.onclicklistener click=new view.onclicklistener(){         @override         public void onclick(view v) {             switch(getposition())             {                 case 1:                     intent = new intent(context, me.class);                     startactivity(intent); // error!             }         }      }; } 

i'm trying put startactivity in it, it's getting error.
how can handle it?

try below.

private view.onclicklistener click=new view.onclicklistener(){             @override             public void onclick(view v) {                 switch(getposition())                 {                     case 1:                         intent = new intent(context, me.class);                         context.startactivity(intent); // or                         v.getcontext().startactivity(intent);                 }             }          }; 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -