当前位置: 动力学知识库 > 问答 > 编程问答 >

How to divide gridview in sections in android?

问题描述:

My question is basic but I couldn't find any answer for my question. I have a layout which contains 2 textviews and a gridview. I added this layout into a listview.I want to generate multiple layouts. For example;

ListView------------------1stElement

----------textview1-----------------

----------gridview1-----------------

----------textview1-----------------

ListView------------------2ndElement

----------textview2-----------------

----------gridview2-----------------

----------textview2-----------------

ListView------------------3rdElement

----------textview3-----------------

----------gridview3-----------------

----------textview3-----------------

I tried it like that;

for(int i = 0; i<category.getChildren().size(); i++){

listView.setAdapter(new CategoryListAdapter(this, category.getChildren().get(i)));

}

But of course, it showed my last view only. setAdapter() doesn't suit to my code. I need something like adding views over and over.

Thanks for your helps.

网友答案:

You don't have to do this in for loop, Adapter Takes whole list Once Only, If u do it in a loop it will set the last list visible, So make list in For loop first and then add i to adapter only once.

for(int i=0;i<size;i++)
{
   ArrayList<Object> myChldrenList  = new ArrayList<Object>();
   Object mychild = mycategory.getChildren().get(i)
   myChildrenList.add(myChild);
}

 listView.setAdapter(new CategoryListAdapter(this, myChildrenList));

u can use Ur Class type Instead of object class or simply Type Cast Object Class.

分享给朋友:
您可能感兴趣的文章:
随机阅读: