0

I have a listview and an array adapter. I want to pass an array and create a list. So far so good. Now the next thing i want to do is append another array resource below the already created list. I can not club the two arrays together because the two are supposed to have different layouts. Is there a way to achieve this?

Jai Saxena
  • 255
  • 1
  • 2
  • 15

1 Answers1

1

yes you can

first of all you need to make a new class that extends ArrayAdapter and override the getview method

 class MyAdapter extends ArrayAdapter {





public MyAdapter(Context context,ArrayList<MyClass> myarray) {
    super(context,R.layout.yourlayout,myarray);


}



    @Override
public View getView(int position, View convertView, ViewGroup parent) {
 /*this is also called when creating for the first time or once any view (row) is visible again    */               


View view=null;
MyClass object=(MyClass)getItem(position);

//if true than choose the first layout
if (object.layout1 == true) {

view=LayoutInflater.from(getContext()).inflate(R.layout.yourlayout1,  
parent, false);
}else if(object.layout2==true){
view=LayoutInflater.from(getContext()).inflate(R.layout.yourlayout2,  
parent, false);

}



}

and now you should have a class for example called MyClass contains

public class MyClass{
 public boolean layout1;
 public boolean layout2;
 }

so this way when you add item into the adapter you add an Object of MyClass and all you have to do is to set the values of boolean layout1 and layout2 to true or false to know which layout you want to choose

and than an example of adding a new item into adpater which you want to it to choose layout2 is:

      MyClass object=new MyClass();
     object.layout1=false;
     object.layout2=true;
     //setting  listview adpater 
     MyListView.setAdapter(new MyAdapter(this,new ArrayList<MyClass>()));

     Myadapter.add((Object)object);
has19
  • 1,307
  • 16
  • 31