2

我有一个列表视图的自定义适配器,其单元格包含一个删除按钮。我想显示一条消息“真的删除这个条目吗?” 按下删除时。

我从自定义适配器内部处理删除按钮事件,问题是为了创建一个对话框,我需要对当前活动的引用......我该如何解决这个问题?仅使用上下文就会导致出现空指针异常。

4

3 回答 3

4

把这段代码...

AlertDialog.Builder alertDialog = new AlertDialog.Builder((Activity) v.getContext());

            alertDialog.setTitle("Delete this item?");
            alertDialog.setMessage("Are you sure you want to delete this?");
            alertDialog.setIcon(R.drawable.icon);

            alertDialog.setPositiveButton(
                "Delete",
                new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Do the stuff..
                        }
                    }
                );


            }

            alertDialog.show();

在您的列表视图上单击侦听器:

@Override
    public void onListItemClick(ListView lv, View v, int position, long id) {

}

是你要找的吗?

于 2013-05-12T11:52:33.373 回答
2

这应该是您的适配器的骨架,以便活动能够将上下文转发给您的适配器。

public class ImagePrepare extends BaseAdapter {

    private Context ctx;

        public ImagePrepare(Context ctx, String[] icons,DisplayMetrics m) {
        // TODO Auto-generated constructor stub
        this.ctx=ctx;

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lengthofdata;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

    }
   }
于 2013-05-12T11:59:07.340 回答
1

你可以这样使用。

lv1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                LayoutInflater layout = (LayoutInflater) getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layout.inflate(R.layout.popup1, null);
                Display display = getWindowManager().getDefaultDisplay();
                int height = display.getHeight();
                int width = display.getWidth();
                final PopupWindow popupWindaow = new PopupWindow(popupView,
                        (int) (width / 1.4), (int) (height / 2.5));
                popupWindaow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

                TextView tv1 = (TextView) popupView
                        .findViewById(R.id.textView1);                                  

                tv1.setText("Really delete this entry?");

                Button No = (Button) popupView.findViewById(R.id.No);
                            Button Yes = (Button) popupView.findViewById(R.id.Yes);
                Yes.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        //code to delete

                    }
                });

                No.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        popupWindaow.dismiss();

                    }
                });

            }
        });

我希望这能帮到您。

于 2013-05-12T11:56:57.910 回答