Set Adapter to linearLayout instead of listView

。_饼干妹妹 提交于 2019-12-11 05:48:12

问题


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.


回答1:


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



回答2:


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




回答3:


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

This information and more can be found here



来源:https://stackoverflow.com/questions/28634232/set-adapter-to-linearlayout-instead-of-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!