Vedran Kopanja
Vedran Kopanja

Reputation: 1299

notifyDataSetChanged never works

I have been searching for ages and trying for months. I can NEVER get my views (ANY view) to invalidate. What's the problem here, can anyone please tell me? And can anyone tell me how to use these invalidate methods. Thanks in advance!

                if(result.equals("true"))
                {
                    View row = invoices.getChildAt(info.position);
                    //TextView bgr = (TextView) row.findViewById(R.id.status);
                    //bgr.setBackgroundResource(R.color.blue);
                    //row.setVisibility(View.GONE);

                    Animation anim = AnimationUtils.loadAnimation(Invoices.this, R.anim.down_to_top);
                    anim.setDuration(500);
                    invoices.getChildAt(info.position).startAnimation(anim );
                    final int row_pos = info.position;

                    new Handler().postDelayed(new Runnable() {

                        public void run() {

                            /*row.getInstance().remove(
                                FavouritesManager.getInstance().getTripManagerAtIndex(info.position)
                            );*/
                            ListView invoices = (ListView) findViewById(R.id.allInvoices);
                            View row = invoices.getChildAt(row_pos);
                            row.setVisibility(View.GONE);
                            getAllInvoices();
                            myAdapter.notifyDataSetChanged();

                        }

                    }, anim.getDuration());

                    Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_SHORT).show();
                }

update: This is what I got now. It removes the view and animates correctly, but it still doesn't invalidate?

                            ListView invoices = (ListView) findViewById(R.id.allInvoices);
                            View row = invoices.getChildAt(row_pos);
                            HashMap<String, String> lRow = invoice_items.get(row_pos);
                            lRow.remove(row);
                            row.setVisibility(View.GONE);
                            myAdapter.notifyDataSetChanged();

Upvotes: 1

Views: 751

Answers (2)

DroidBender
DroidBender

Reputation: 7892

The reason why the call notifyDataSetChanged is not working, is because the data set is not changed.

Inside your Handler, you alter the layout of the ListView itself. The ListView is not more than a representation of an Adapter, which holds data.

To understand how it works, this post will explain it.

Applying that on your code would output something like this: (PSUEDO)

Handler {
    lRow = mAdapter.get(row_pos);
    lRow.setVisibilty(false); // visibility is a flag of the row object
    mAdapter = notifyDataSetChanged();
}

inside Adapter:

getView() {
   if (row.getVisibility){
       // do stuff
   } else {
       // do other stuff 
   }
}

Upvotes: 1

TNR
TNR

Reputation: 5869

Hey Check you Handler run() where you are creating ListView again and not setting the Adapter. you might be notifying the adapter but that adapter is not set to Listview as you creating another object. I don't know what your getAllInvoices() would do but I am sure that you are not setting adapter to listview so it will never get notified. as per your code you cannot set the adapter even in getAllInvoices() as listview is local variable.

Upvotes: 0

Related Questions