简体   繁体   English

Android:如何在删除行后刷新tablelayout?

[英]Android: How to refresh a tablelayout after removing a row?

I have a tablelayout that retrieves data from a *.txt file. 我有一个tablelayout,可以从* .txt文件中检索数据。 For every line of data in the txt file, there will be one row of data. 对于txt文件中的每一行数据,都会有一行数据。

Let's say I have two rows of data in the txt file right now, it makes sense that two tablerows will be generated. 假设我现在在txt文件中有两行数据,有意义的是会生成两个表格。

Then, I added a OnLongPressListener which, when called, will delete one row of data from the txt file. 然后,我添加了一个OnLongPressListener,当被调用时,它将从txt文件中删除一行数据。

Now's the first question: After deleting data in the txt file, how do I refresh my tablelayout to reflect that change? 现在是第一个问题:在删除txt文件中的数据后,如何刷新tablelayout以反映该更改?

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! 谢谢!

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 ). 加上我是被迫的事实,使用TableLayout (代码编写的使用量TableLayout是巨大的,这不是我的电话切换到ListView )。 What I ended up doing was to remove all the views from TableLayout using: 我最终做的是使用以下方法从TableLayout删除所有视图:

`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);

Why not use a ListView instead? 为什么不使用ListView呢? It gives you better control when using an MVC model where there's a dataset tied to a View . 当使用与View绑定的数据集的MVC模型时,它可以更好地控制。 So you could have a class that extends BaseAdapter . 所以你可以有一个扩展BaseAdapter的类。 This class binds your data to the View and this class also has a method: notifyDataSetChanged() which is what should solve your problem. 这个类将你的数据绑定到View ,这个类也有一个方法: notifyDataSetChanged() ,它应该解决你的问题。

You may find notifyDataSetChanged example and customListView helpful. 您可能会发现notifyDataSetChanged示例customListView很有帮助。

Let me know if you need any help further. 如果您需要进一步的帮助,请告诉我。

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. 通常在onCreate()中我们做所有用文本显示ui的东西,尝试将构成UI的代码放在私有方法中说setUpTabView()并尝试从onCreate调用它,甚至尝试调用此setUpTabView()时文字改变了。 This kind of approach i did in grid view. 我在网格视图中做的这种方法。 worked very well...! 工作得很好......!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM