3

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!

Danny
  • 31
  • 1
  • 1
  • 2
  • 1
    You need to accept answers that worked for you so that next time you ask questions - people don't hesitate to post answers! – Sagar Hatekar Apr 20 '11 at 03:18

3 Answers3

4

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);
Jermin Bazazian
  • 1,932
  • 2
  • 17
  • 20
1

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.

Community
  • 1
  • 1
Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72
  • I agree list is a much better way to go. and a possible animation solution would be easier to find/create. – Wayner Apr 15 '11 at 18:09
  • Alright I tried your suggestion but it's nearly impossible (or I just don't know how to do it right) to convert my setup to listview. What I have is a background picture of a resistor and four textview (each with no text but different background colours). You see how that's working? I tried applying listview and customizing my own simple_list.xml to have a background of a resistor. But I don't know how to add four textview to one row of listview. – Danny Apr 15 '11 at 18:50
  • Okay. I don't know how your four TextViews will appear but row.xml in the tutorial already has two TextViews so you just add two more, shouldn't that be easy? – Sagar Hatekar Apr 15 '11 at 20:13
  • Accept the answer that helped you so that answers may know what worked for you. Happy Coding! – Sagar Hatekar Apr 18 '11 at 04:16
1

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

gvk51
  • 131
  • 1
  • 4