showDialog() 在适配器中不起作用

showDialog() is not working within an adapter

我试图在单击我的适配器中的 ImageView 时调出一个对话框。我已经尝试了几件事,但还没有成功。现在,我正在尝试像在 Activity 中那样使用 showDialog() 但没有成功。找不到 showDialog() 方法。

适配器:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String[] mStringArray = new String[mData.size()];
        mStringArray = mData.toArray(mStringArray);
        final Integer recordID = Integer.parseInt(mStringArray[position]);
        Cursor res = mydb.getData(recordID);
        ImageView cancel = (ImageView) convertView.findViewById(R.id.x);
        res.close(); // that's important too, otherwise you're gonna leak cursors

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityContext.showDialog(DIALOG_ALERT);
                mydb.deleteSavedVideo(recordID);
                mData.remove(position);
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

适配器:

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_ALERT:
                AlertDialog.Builder builder = new AlertDialog.Builder(activityContext);
                builder.setTitle("Remove Video");
                builder.setMessage("hkjh.");
                builder.setCancelable(true);
                builder.setPositiveButton("Cancel", new CancelOnClickListener());
                builder.setNegativeButton("Remove", new deleteRow());
                AlertDialog dialog = builder.create();
                dialog.show();
        }
        return super.onCreateDialog(id);
    }

已更新

适配器构造函数

public SavedVideoAdapter(Context context, ArrayList<Object> data, MySQLiteHelper database) {
    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = data;
mydb = database;
activityContext = context;

}

Activity:

public void getSavedVideos() {
        mydb = new MySQLiteHelper(getActivity());
        listView.setEmptyView(rootview.findViewById(R.id.noSavedVideosTextView));

        //Get IDs of all rows in the db
        ArrayList savedVideoIDs = mydb.getAllSavedVideo();

        mSavedVideoAdapter = new SavedVideoAdapter(getActivity(), savedVideoIDs, mydb);
        listView.setAdapter(mSavedVideoAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent myIntent = new Intent(getActivity(), UploadVideoActivity.class);
                startActivity(myIntent);
            }
        });
    }

不需要 AlertDialog dialog = builder.create();dialog.show(); 只需 builder.show(); 即可。

您在 Adapter class 中定义了 onCreateDialog。您需要在 Activity class 中添加此方法才能访问 Activity context.

问题最有可能与以下调用有关:

activityContext.showDialog(DIALOG_ALERT);

您如何从 AdaptergetView 方法中的主要 Activity 获取 Context?为了解决这个问题,只需将 Context 从您的主要 Activity 传递给您的 Adapter 并使用所说的 Context 作为您的 Dialog.

编辑

我认为问题在于您在 Adapter 中使用了 onCreateDialog,您不能这样做,因为 onCreateDialog 属于 Activity class .尝试将 onCreateDialog 放在主 Activity 中,然后在 Adapter.

中显示 Dialog

此外,当您实例化 Adapter 时,请尝试使用:

mSavedVideoAdapter = new SavedVideoAdapter(this, savedVideoIDs, mydb);

而不是...

mSavedVideoAdapter = new SavedVideoAdapter(getActivity(), savedVideoIDs, mydb);