从自定义适配器打开一个对话框

时间:2013-05-12 11:36:24

标签: android

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

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

3 个答案:

答案 0 :(得分:3)

把这段代码......

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) {

}

你在寻找什么?

答案 1 :(得分: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

    }
   }

答案 2 :(得分: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();

                    }
                });

            }
        });

我希望这会对你有所帮助。