如何在适配器中正确显示自定义对话框

问题描述 投票:0回答:1

我在我的应用程序中有一个自定义适配器,我从那里启动了几个活动,适配器中的填充列表之一将您带回登录屏幕,该屏幕工作正常,但是我决定在带用户之前添加一个确认对话框到登录屏幕,但出现错误,提示android.content.ActivityNotFoundException: No Activity found to handle Intent { }

这是我到目前为止已经实现的

AccountAdapter.java

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView title;
        ImageView icon;
        MyViewHolder(View view) {
            super(view);
            view.setClickable(true);
            view.setOnClickListener(this);
            mCtx = view.getContext();
            title = view.findViewById(R.id.title);
            icon = view.findViewById(R.id.icon);
        }
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            switch (getAdapterPosition()){
                case 0:
                    intent =  new Intent(mCtx, SettingsActivity.class);
                    break;
                case 1:
                    intent =  new Intent(mCtx, ProfileActivity.class);
                    break;
                case 2:
                    intent =  new Intent(mCtx, ChangePasswordActivity.class);
                    break;
                default:
                    CustomAlertDialog alert = new CustomAlertDialog((Activity)mCtx);//faulty code
                    alert.show();
                    break;
            }
            mCtx.startActivity(intent);
        }
    }

CustomAlertDialog.java

public class CustomAlertDialog extends Dialog implements android.view.View.OnClickListener {

    public Activity c;
    public Dialog d;
//    public Button yes, no;
    public TextView yes, no;

    public CustomAlertDialog(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_alert_dialog);
        yes = (TextView) findViewById(R.id.btn_yes);
//        no = (Button) findViewById(R.id.btn_no);
        yes.setOnClickListener(this);
//        no.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_yes:
                break;
            default:
                break;
        }
        dismiss();
    }
}

AccountFragment.java(在其中填充帐户适配器)

private void prepareAccountData() {
//        if (mAdapter == null) {
//            return;
//        }
        AccountModel options = new AccountModel("Settings", R.drawable.ic_action_settings);
        accountList.add(options);
        options = new AccountModel("Update Profile", R.drawable.ic_social_person_outline);
        accountList.add(options);
        options = new AccountModel("Change Password", R.drawable.ic_hardware_security);
        accountList.add(options);
        options = new AccountModel("Logout", R.drawable.ic_action_exit_to_app);
        accountList.add(options);
        mAdapter.notifyDataSetChanged();
    }

这是我用于直接注销的代码,也可以正常工作

((Activity)mCtx).finish();
                    SharedPreferences sharedPreferences = mCtx.getSharedPreferences("key", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.clear();
                    editor.apply();
                    intent =  new Intent(mCtx, LoginActivity.class);

如何在适配器中实现自定义对话框?

java android android-alertdialog
1个回答
0
投票

在类之间传递活动可能会导致内存泄漏,并可能导致运行时异常,因为传递的活动可能在您决定退出类之前被销毁。

© www.soinside.com 2019 - 2024. All rights reserved.