-2

I am going to populate a scroll view with a dynamic number of child LinearLayouts at runtime. I am trying to formulate the cleanest way to do this.

The child layout will contain two text items. The text items will be set to display the savefile name and savefile date at runtime.

So my question is, can I create a single xml layout, and then use a LayoutInflater to create multiple instances of this layout at runtime?

If I could, how would I gain access to multiple instances of text1 and text2. It seems like this would break the whole findViewById model.

It just seems that creating lots of UI elements at runtime in android is messy and verbose (setting all the layout options for the view when you create them)....

in infomercial style

There has got to be a better way!! :P

Scorb
  • 1,654
  • 13
  • 70
  • 144

1 Answers1

2

I am going to populate a scroll view with a dynamic number of child LinearLayouts at runtime. I am trying to formulate the cleanest way to do this.

can I create a single xml layout, and then use a LayoutInflater to create multiple instances of this layout at runtime?

Sounds like what you need is a ListView with a custom ArrayAdapter. Each object in the adapter binds to a single xml layout. Adding objects to the adapter will dynamically add views to the list. And listviews are scrolling. So, all checkboxes of your question are satisfied.

how would I gain access to multiple instances of text1 and text2. It seems like this would break the whole findViewById model

The inflater.inflate approach returns a View, and findViewById is a method of View, so nothing is "broken". You would need some arraylist of references to each View object, which you can findViewById on.

However, that is messy, and the adapter approach is the "cleanest" way.

Alternatively, there is RecyclerView instead of ListView, and it uses a different Adapter, but I'm just sticking to the native Android library. The concept is the same, though.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • an ArrayAdapter seems to be for configuring the data to the listview. But what I am more concerned about is the layout of each item in the list view. Based on this http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view it seems like using a CustomAdapter is more work than inflating multiple instances of an xml. – Scorb May 07 '16 at 03:42
  • What do you mean by more work? That step is required and included in what you just linked to. `vi.inflate(R.layout.itemlistrow, null);` – OneCricketeer May 07 '16 at 03:46
  • You are correct. Extending BaseAdapter is the correct way to do this. Thanks. – Scorb May 07 '16 at 03:59
  • Hey, no need to be rude to me if the voice in your head decides to read something as snotty. Anyways, `ArrayAdapter` just implements some of the more useful methods of `BaseAdapter` such as `getItem` and `getCount`. It also provides `add` and `remove` for dynamically messing with the data it contains – OneCricketeer May 07 '16 at 04:25