How to finish an activity from BaseAdapter in android? -


explanation: know repeated question.understand flow of application. have multiple fragments in navigationdrawer when in 1 of fragment.this fragment call baseadapter adapter , select value fragment.after selected item fragment goes mainactivity

here adapter code

package com.angelnx.angelnx.holidays.adapter;   import android.app.activity; import android.content.context; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.radiobutton; import android.widget.textview; import android.widget.toast;  import com.angelnx.angelnx.holidays.handler.databasehandler; import com.angelnx.angelnx.holidays.mainactivity; import com.angelnx.angelnx.holidays.r; import com.angelnx.angelnx.holidays.model.state; import com.angelnx.angelnx.holidays.model.users;  import java.util.list;  public class stateselectionadapter extends baseadapter{      private context context;     private list<state> state_list;     private int mselectedvariant;     private layoutinflater inflater;     private databasehandler db;      public stateselectionadapter(context context,list<state> state_list,int mselectedvariant){         this.context=context;         this.state_list=state_list;         this.mselectedvariant=mselectedvariant;         inflater=(layoutinflater)context.getsystemservice(context.layout_inflater_service);     }     @override     public int getcount() {         return state_list.size();     }      @override     public object getitem(int position) {         return state_list.get(position);     }      @override     public long getitemid(int position) {         return position;     }      private class holder{         textview state_name;         radiobutton radiobutton;     }      @override     public view getview(final int position, view convertview, viewgroup parent) {         holder holder=new holder();          if(convertview==null){             convertview=inflater.inflate(r.layout.radiolistviewrow,null);              holder.state_name=(textview)convertview.findviewbyid(r.id.tv_maintext);             holder.radiobutton=(radiobutton)convertview.findviewbyid(r.id.rb_choice);              holder.state_name.settext(this.state_list.get(position).getstate_name());             if(holder.radiobutton.ischecked()){              }             convertview.settag(holder);         }         else{             holder=(holder)convertview.gettag();         }          if(position==mselectedvariant){             holder.radiobutton.setchecked(true);         }         else{             holder.radiobutton.setchecked(false);         }          convertview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 mselectedvariant=position;                 db=new databasehandler(context);                  users user=new users();                 user.setid("1");                 user.setselectstate(state_list.get(position).getid());                 int no=db.updateuser(user);                 if(no > 0){                     toast.maketext(context, "your state is:"+state_list.get(position).getstate_name(), toast.length_short).show();                 }                 else{                     toast.maketext(context, "your state"+state_list.get(position).getstate_name(), toast.length_short).show();                 }                 stateselectionadapter.this.notifydatasetchanged();                 intent i=new intent(context, mainactivity.class);                 i.setflags(intent.flag_activity_clear_top|intent.flag_activity_new_task);                 context.startactivity(i);                 ((activity)(context)).finish();             }         });         return convertview;     } } 

here error got

fatal exception: main

process: com.angelnx.angelnx.holidays, pid: 14827 java.lang.classcastexception: com.angelnx.angelnx.holidays.app.myapplication cannot cast android.app.activity @ com.angelnx.angelnx.holidays.adapter.stateselectionadapter$1.onclick(stateselectionadapter.java:105) @ android.view.view.performclick(view.java:4438) @ android.view.view$performclick.run(view.java:18422) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ dalvik.system.nativestart.main(native method) 

here singleton class namely myapplication.java

package com.angelnx.angelnx.holidays.app;   import android.app.application; import android.content.intent; import android.text.textutils;  import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.toolbox.volley; import com.angelnx.angelnx.holidays.loginactivity; import com.angelnx.angelnx.holidays.helper.mypreferencemanager;  public class myapplication extends application{     public static final string tag = myapplication.class             .getsimplename();      private static myapplication minstance;     private requestqueue mrequestqueue;     private mypreferencemanager pref;      @override     public void oncreate() {         super.oncreate();         minstance = this;     }      public static synchronized myapplication getinstance() {         return minstance;     }     public mypreferencemanager getprefmanager() {         if (pref == null) {             pref = new mypreferencemanager(this);         }          return pref;     }     public requestqueue getrequestqueue() {         if (mrequestqueue == null) {             mrequestqueue = volley.newrequestqueue(getapplicationcontext());         }          return mrequestqueue;     }     public <t> void addtorequestqueue(request<t> req, string tag) {         req.settag(textutils.isempty(tag) ? tag : tag);         getrequestqueue().add(req);     }      public <t> void addtorequestqueue(request<t> req) {         req.settag(tag);         getrequestqueue().add(req);     }      public void cancelpendingrequests(object tag) {         if (mrequestqueue != null) {             mrequestqueue.cancelall(tag);         }     }  } 

pass activity reference context in baseadapter class. work perfectly.

here passing application reference context application not casting activity.

use activity reference creating instance of adapter , if creating adapter in fragment use getactivity() method context.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo