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

时间:2015-01-17 19:42:16

标签: android android-listview

当我点击适配器中的ImageView时,我试图调出一个对话框。我尝试了几件事但没有让它发挥作用。现在,我正在尝试使用showDialog(),就像我在一个Activity中一样但没有运气。它找不到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;

}

的活动:

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);
            }
        });
    }

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

问题很可能出现以下问题:

activityContext.showDialog(DIALOG_ALERT);

如何通过Context的{​​{1}}方法从主Activity获得getView?要解决此问题,只需将主Adapter的{​​{1}}表单传递到Context,然后将Activity用于Adapter

修改

我认为问题是您在Context中使用Dialog,因为onCreateDialog属于Adapter类,所以您无法执行此操作。尝试将onCreateDialog放入主Activity,然后在onCreateDialog中显示Activity

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

Adapter

而不是......

Adapter