简体   繁体   中英

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

I have a Listview and a

setOnItemClickListener(new AdapterView.OnItemClickListener() on it.

I click and an AlertDialog warns me if I trully want to disable this item or not.

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

            }
        }
    });

If I click Yes then I get NullPointerException on null object reference what is getting null here?

Could you please try following changes and let me know if works?

NOTE If does not work, I'll delete the answer. I'm putting here since it is too big to add it in the comments:

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

Your code may not work well

You are using a ListView and the views are re-used. This way, eventually, a disabled View may be used in a position which should receive a enabled view (and vice-versa)....

This is easy to check if you do following test:

  • Disable first view...
  • Then, scroll until a non-visible item becomes visible...
  • Then, you will note that one view may appear disabled also (but you disabled only the first view).

So, in fact, you should use a different approach:

Best Way

Note, this is just an example... Just to share the idea:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM