Android: How To Refresh A Tablelayout After Removing A Row?
Solution 1:
Not sure if you are still looking for answer. But I came across this post facing kinda the same problem. Plus the fact that I was forced to use TableLayout
(the amount of code written using TableLayout
is huge and it wasn't my call to switch to ListView
).
What I ended up doing was to remove all the views from TableLayout
using:
`tableLayout.removeAllViews();`
But that's not gonna work if the number of rows after removal changes drastically. I needed to invalidate my view too, using a handler. Here is the rest of my code.
protectedstatic final int REFRESH = 0;
privateHandler _hRedraw;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tree_view_activity);
_hRedraw=newHandler(){
@OverridepublicvoidhandleMessage(Message msg)
{
switch (msg.what) {
caseREFRESH:
redrawEverything();
break;
}
}
};
...
}
privatevoidredrawEverything()
{
tableLayout.invalidate();
tableLayout.refreshDrawableState();
}
There is only one part left and that's the part where you send a message to your handler to refresh your view. Here is the code for it:
_hRedraw.sendEmptyMessage(REFRESH);
Solution 2:
Why not use a ListView
instead? It gives you better control when using an MVC model where there's a dataset tied to a View
. So you could have a class that extends BaseAdapter
. This class binds your data to the View
and this class also has a method: notifyDataSetChanged()
which is what should solve your problem.
You may find notifyDataSetChanged example and customListView helpful.
Let me know if you need any help further.
Solution 3:
Generally in onCreate() we do all the stuff that shows the ui with text, try to put the code that makes up UI in a private method say setUpTabView() and try to call this from onCreate and even try calling this setUpTabView() when ever the text changed. This kind of approach i did in grid view. worked very well...!
Post a Comment for "Android: How To Refresh A Tablelayout After Removing A Row?"