[英]How to finish an activity from BaseAdapter in android?
Explanation: I know this is my repeated question.Understand the flow of my application. 说明:我知道这是我重复的问题。了解我的应用程序的流程。 I have multiple fragments in my navigationDrawer when i am in one of my fragment.This fragment call a BaseAdapter adapter and select the value from fragment.After selected an item from my fragment i goes to my mainActivity
当我在一个片段中时,我的NavigationDrawer中有多个片段。这个片段调用BaseAdapter适配器并从片段中选择值。从片段中选择一个项目后,我进入了mainActivity
Here is my 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 is an error which i got 这是我得到的错误
FATAL EXCEPTION: main 致命异常:主要
Process: com.angelnx.angelnx.holidays, PID: 14827
java.lang.ClassCastException: com.angelnx.angelnx.holidays.app.MyApplication cannot be cast to android.app.Activity
at com.angelnx.angelnx.holidays.adapter.StateSelectionAdapter$1.onClick(StateSelectionAdapter.java:105)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
here is my singleton class namely MyApplication.java 这是我的单例类,即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 the Activity
reference as a Context
in BaseAdapter
class. 将
Activity
引用作为Context
传递给BaseAdapter
类。 That will work perfectly. 那将完美地工作。
Here you are passing Application
reference as Context
so the Application
is not casting to Activity
. 在这里,您将
Application
引用作为Context
传递,因此Application
不会转换为Activity
。
Use the Activity
reference where you are creating the instance of this adapter and if you are creating this Adapter
in Fragment
then use getActivity()
method as context. 在要创建此适配器的实例的位置使用
Activity
参考,如果要在Fragment
中创建此Adapter
,请使用getActivity()
方法作为上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.