1

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();
Vedran Kopanja
  • 1,259
  • 12
  • 23

2 Answers2

1

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 
   }
}
Community
  • 1
  • 1
DroidBender
  • 7,762
  • 4
  • 28
  • 37
  • 1
    Updated question :) It removes everything, but still not invalidating, so I have a hole in the middle of ListView hehe. **edit:** I just removed item from my HashMap, that's probably why it isn't working... But how the hell do I remove it from Adapter?! – Vedran Kopanja Dec 07 '12 at 12:25
  • 1
    You feed some kind of collection to your Adapter. For example an Array, a List, a HashMap. Inside your Adapter you can control how the ListView will present the data in his View. If you remove an object from the collection that you feed to your Adapter, you can refresh the Adapter with notifyDataSetChanged and he will on his turn update the ListView. – DroidBender Dec 07 '12 at 12:40
0

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.

TNR
  • 5,839
  • 3
  • 33
  • 62
  • 1
    But I'm using the same adapter, the ListView in run() is used only to see which row I need to hide. Also I just tried making my ListView outside of onCreate and then initializing it in onCreate, then using it everywhere else, that didn't work also. – Vedran Kopanja Dec 07 '12 at 11:53