1

So I'm working in Android studio, with Java as my language, and I've got an embedded database. I'd like to press some button on my app to display everything that's in my database at that point in time. Let's assume my database carries letter-number combinations, eg "A,45", "B,57" etc

Now to display this data in my app I'm using the following method:

LinearLayout my_layout = (LinearLayout)findViewById(R.id.mainView);

    Cursor c = dbase.query("SELECT letter, number from database");

    while (c.moveToNext()){
        TextView t = new TextView(this);
        t.setText(c.getString(c.getColumnIndex("letter"))+","+c.getLong(c.getColumnIndex("number")));
        my_layout.addView(t);
    }
    c.close();

Now this works fine to display my data the first time I press the button on my app, but the problem is on subsequent presses the already displayed data remains on screen, and eventually the screen runs out of space.

Eg, press once:

A,45
B,57

press again:

A,45
B,57
A,45
B,57

and so on.

How can I modify my loop so that on each new press the current contents of the screen are erased? Any help appreciated.

1 Answer 1

0

You can remove all views from the Layout before you repopulate it:

LinearLayout my_layout = (LinearLayout)findViewById(R.id.mainView);
my_layout.removeAllViews();

Cursor c = dbase.query("SELECT letter, number from database");
while (c.moveToNext()){
    TextView t = new TextView(this);
    t.setText(c.getString(c.getColumnIndex("letter"))+","+c.getLong(c.getColumnIndex("number")));
    my_layout.addView(t);
}
c.close();

Or if there other views inside the Layout use a list to store the generated TextViews which will be cleared and repopulated whenever you click the button.
So define the list at the activity class level:

List<TextView> list;

and initilize it in onCreate():

list = new ArrayList<>();

then modify your listener like this:

LinearLayout my_layout = (LinearLayout) findViewById(R.id.mainView);
for (TextView t : list) {
    my_layout.removeView(t);
}
list.clear();

Cursor c = dbase.query("SELECT letter, number from database");
while (c.moveToNext()){
    TextView t = new TextView(this);
    t.setText(c.getString(c.getColumnIndex("letter"))+","+c.getLong(c.getColumnIndex("number")));
    my_layout.addView(t);
    list.add(t);
}
c.close();
5
  • Thanks, but that would prevent me from engaging with my layout more than once. When I press my button for the first time only the data is present on the screen, all my objects disappear
    – Mike
    Commented Apr 23, 2021 at 18:41
  • @Mike what other objects?
    – forpas
    Commented Apr 23, 2021 at 18:42
  • Well most importantly the button I'm using for this method which calls and displays the data, and also the other buttons/edit text objects I'm using. Press button - data shows up but it's now the only thing present on the screen
    – Mike
    Commented Apr 23, 2021 at 18:45
  • @Mike you do not mention in the question that this Layout contains other views. Why don't you have another layout inside to store all the generated TextViews? It would be the simplest solution. As it is, you will have to store all the generated TextViews in a List so that on any click of the button you can remove them 1 by 1 in a loop. This is more complicated.
    – forpas
    Commented Apr 23, 2021 at 18:48
  • @forbas I just created a secondary linear layout to store the textviews, works perfectly now along with your suggestion to remove views before repopulating. Thanks so much
    – Mike
    Commented Apr 23, 2021 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.