列表视图中的按钮不起作用alertDialog

时间:2019-06-19 09:06:30

标签: android android-4.4-kitkat

我的目标是,如果我单击button中的listview,然后工作AlertDialog。 没有日志或错误。即使单击该按钮,应用也可以。 在listview onItemClick上,我们可以进行对话。 请发表评论。

我阅读了注释,并显示了我的代码。我用适配器控制listview

所以我有mainActivity.java和listadapter.java,我在adapter.java中设置了listBtnClickListener,在mainActivity.java中设置了Override。

我想制作AlertBuilder,但不起作用。

main.java

    @Override
public void onListBtnClick(final int position, int id) {
    final int pos = position;
    if(adapter.getId(1) == id){

        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.contact_add, null);
        builder.setView(view);
        builder.setTitle("add");

        builder.setNegativeButton("cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setLayout(400,250);

    }
}

adapter.java

       @Override
public View getView(int position, View convertView, ViewGroup parent) {
    callBtnId = 1;
    delBtnId = 3;
    final int pos = position;
    context = parent.getContext();

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(this.resourceId/*R.layout.contact_listview_item*/, parent, false);
    }

    nameTextView = (TextView) convertView.findViewById(R.id.textView1);
    phoneTextView = (TextView) convertView.findViewById(R.id.textView2);

    listViewItem = (ListViewItem) getItem(position);

    nameTextView.setText(listViewItem.getName());
    phoneTextView.setText(listViewItem.getPhone());

    Button button1 = (Button) convertView.findViewById(R.id.callBtn);
    button1.setTag(position);
    button1.setOnClickListener(this);
    callBtnId = button1.getId();

    return convertView;
}

public void onClick(View v) {
    if (this.listBtnClickListener != null) {
        this.listBtnClickListener.onListBtnClick((int) v.getTag(),v.getId());
    }
}

0 个答案:

没有答案