Nikitha Reddy
Nikitha Reddy

Reputation: 11

Set Adapter to linearLayout instead of listView

I am working on a layout that needs to adapt to the kind of data I receive. I need to make this as generic as possible. So the layout in my adapter class needs to be modified dynamically according to the data. This is how I plan to do so.

My activity xml layout:

<LinearLayout>
  <LinearLayout id="1" ></LinearLayout>
  <LinearLayout id="2" ></LinearLayout>
  <LinearLayout id="3" ></LinearLayout>
  <LinearLayout id="4" ></LinearLayout>
  <LinearLayout id="5" ></LinearLayout>
</LinearLayout>

I have adapter which populates data. Adapter code 1. Inflate new layout N1 2. Get id of activity LinearLayout based on adapter item position 3. Put N1 in parent LinearLayout Slot

Problem. How do I use Static LinearLayout than ListView.

Upvotes: 1

Views: 4818

Answers (3)

Egor Mikhailov
Egor Mikhailov

Reputation: 29

You can do this:

LinearLayout holder = new LinearLayout(context);
...
holder.removeAllViews();
for(int i = 0 ; i < adapter.getCount(); i++) 
    holder.addView(adapter.getView(i, null, holder));
...

Upvotes: 1

Shane Denomme
Shane Denomme

Reputation: 105

If my understanding is correct you should be able to use a SimpleAdapter and a list view to handle what you need. Is there any specific reason you need to use a linear layout as the parent view?

"An adapter manages the data model and adapts it to the individual entries in the widget. An adapter extends the BaseAdapter class.

Every line in the widget displaying the data consists of a layout which can be as complex as you want. A typical line in a list has an image on the left side and two text lines in the middle as depicted in the following graphic."

example

This information and more can be found here

Upvotes: 0

Danny Buonocore
Danny Buonocore

Reputation: 3777

You could use a ListView and just supply a custom layout to the rows

Upvotes: 0

Related Questions