Android: How to refresh a tablelayout after removing a row?

时光毁灭记忆、已成空白 提交于 2019-12-12 15:15:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!