问题
I have a tablelayout that retrieves data from a *.txt file. For every line of data in the txt file, there will be one row of data.
Let's say I have two rows of data in the txt file right now, it makes sense that two tablerows will be generated.
Then, I added a OnLongPressListener which, when called, will delete one row of data from the txt file.
Now's the first question: After deleting data in the txt file, how do I refresh my tablelayout to reflect that change?
Second question is: After I get my first question solved, is it possible to have some kind of animation where one row will fade out or slide out instead of just disappearing outright?
Thanks!
回答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.
protected static final int REFRESH = 0;
private Handler _hRedraw;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tree_view_activity);
_hRedraw=new Handler(){
@Override
public void handleMessage(Message msg)
{
switch (msg.what) {
case REFRESH:
redrawEverything();
break;
}
}
};
...
}
private void redrawEverything()
{
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);
回答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.
回答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...!
来源:https://stackoverflow.com/questions/5680559/android-how-to-refresh-a-tablelayout-after-removing-a-row