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

时间:2016-07-10 14:04:23

标签: android listview android-alertdialog

我有一个Listview和一个

setOnItemClickListener(新的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

1 个答案:

答案 0 :(得分:0)

您可以尝试以下更改并让我知道是否有效吗?

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

@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并重新使用视图。这样,最终,禁用的View可以在一个应该接收启用视图的位置使用(反之亦然)....

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

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

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

最佳方式

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

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);
}