如何在android中将自定义对话框作为listview itemClickListener

时间:2016-04-18 05:37:41

标签: android onitemclicklistener customdialog

我想在我的android项目中使用自定义对话框,点击listview的项目

我在differt类中自定义我的对话框如下:

 public class CustomDialogOption extends Dialog {
    public Activity activity;
    public Button dialog_delete, dialog_edit;
    public TextView dialog_msg;

    public CustomDialogOption (Activity a) {
        super(a);
        this.activity = a;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_counting);

        dialog_msg = (TextView) findViewById(R.id.dialogContet);
        dialog_delete = (Button) findViewById(R.id.delete);
        dialog_edit = (Button) findViewById(R.id.edit);




        dialog_msg.setText("..." + user_number.get(i) + " "
                + user_vow.get(i) + " " + user_why.get(i));
        dialog_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(
                        getApplicationContext(),
                        user_number.get(i) + " "
                                + " delete..", Toast.LENGTH_LONG).show();


            }
        });

我希望在listview上显示此对话框,点击如下:MainActiviyclass

public class MainActivity extends AppCompatActivity  {
private ListView listView;


public final static ArrayList<String> user_number = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //remain numbers -item click listener for Done-vow
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {


            CustomDialogOption cdo = new CustomDialogOption(MainActivity.this ,i);
            cdo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            cdo.show();
        }
    });

}

我的问题是我如何在OnItemClickListener方法(int i)和CustomDialogOption类之间建立关系。 “i”参数未定义。我能做什么?谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

在CustomDialogOption中定义一个int i或其名称。

int i作为param添加到像

这样的构造函数中
 public CustomDialogOption (Activity a, Int i) {
            super(a);
            this.activity = a;
            this.i = i;    
        }

并像这样称呼它

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
            CustomDialogOption cdo = new CustomDialogOption(MainActivity.this, i);
            cdo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            cdo.show();
        }
    });