在 Android 中使用 AlertDialog 时无法禁用 ListView 项

I cannot disable a ListView item when I use an AlertDialog in Android

我有一个列表视图和一个

setOnItemClickListener(new AdapterView.OnItemClickListener()就可以了。

如果我真的想禁用此项目,我会点击 AlertDialog 警告我。

listCustomer.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            if (listCustomer.getChildAt(position).isEnabled()) {


                AlertDialog.Builder builder = new AlertDialog.Builder(DisplayDiscounts.this);
                builder.setTitle("Confirmation");

                builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "No is clicked", Toast.LENGTH_LONG).show();

                    }
                });
                builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {



                db = new DbHelper(getBaseContext());
                Customer myCustomerWithDiscount = db.getCustomer(Integer.parseInt(CustomerId));
                String discount_credits = txtdiscount_credits.getText().toString();
                myCustomerWithDiscount.setCredits(myCustomerWithDiscount.getCredits() - Integer.parseInt(discount_credits));
                db.updateCustomer(myCustomerWithDiscount);

                adapter.notifyDataSetChanged();
                listCustomerDiscounts.setAdapter(adapter);
                        listCustomerDiscounts.getChildAt(position).setEnabled(false);
                        db.closeDB();


                    listCustomer.getChildAt(position).setEnabled(false);

                    }
                });
                builder.show();

            }
        }
    });

如果我单击“是”,那么我会在空对象引用上出现 NullPointerException 什么是空对象?

能否请您尝试以下更改并告诉我是否有效?

注意 如果不起作用,我会删除答案。我把它放在这里,因为它太大了,无法在评论中添加它:

@Override
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {

    if (view.isEnabled()) {
        ...
        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                view.setEnabled(false);
            }
        });
        builder.show();
    }
}});

您的代码可能无法正常工作

您正在使用 ListView 并且视图被重新使用。这样,最终,禁用的视图可以用在应该接收启用视图的位置(反之亦然)....

如果您进行以下测试,这很容易检查:

  • 禁用第一个视图...
  • 然后,滚动直到一个不可见的项目变为可见...
  • 然后,您会注意到一个视图也可能显示为禁用(但您只禁用了第一个视图)。

所以,事实上,你应该使用不同的方法:

最佳方式

注意,这只是一个示例...只是为了分享想法:

Activity.java

@Override
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {

    if (view.isEnabled()) {
        ...
        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Inform the adapter that following item should be disabled
                mAdapter.setEnableState(position, false);
                mAdapter.notifyDataSetChanged();
            }
        });
        builder.show();
    }
}});

Adapter.java

public void setEnableState(int position, boolean state) {
    // boolean array to track view states
    arrayWithStateOfViews[position] = state;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    .....

    // Following code could be simplified to convertView.setEnabled(arrayWithStateOfViews[position])
    if(arrayWithStateOfViews[position] == true)
        convertView.setEnabled(true);
    else
        convertView.setEnabled(false);
}