0

I am trying to group my ListView items using a SimpleAdapter. I am using two Layouts , r1 and r2. I have done the following coding, but I am getting only the contents of layout r2 displayed in my ListView. Can anyone guide me where I am going wrong? My codes are as below:

 adapter=new SimpleAdapter(MainActivity.this, arraylist,R.layout.r1,new String[]{"key1","key2"},new int[]{R.id.textView1,R.id.textView2})
    {



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v=super.getView(position, convertView, parent);

            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(MainActivity.this.LAYOUT_INFLATER_SERVICE);
                int type = getItemViewType(position);
                Log.i("position + convertview + type ",""+position+","+convertView+","+type);
                if(convertView==null)
                {

                     v = inflater.inflate(R.layout.r2, parent, false);
                }
                else
                {
                     v = inflater.inflate(R.layout.r1, parent, false);
                }



                return v;

        }

    };

    l.setAdapter(adapter);

2 Answers2

0

In getView have

 if (type == FIRST_TYPE) {
       //infalte layout of type1
 }else if( type == SECOND_TYPE){
      //infalte layout of type2
 }

Your type corresponds to.

private static final int FIRST_TYPE= 0;
private static final int SECOND_TYPE = 1;

You can have the header based on the position.

@Override
public int getItemViewType(int position) {

    if (position== 0){
        type = FIRST_TYPE;
    } else if  (position == 1){
        type = SECOND_TYPE;
    }
    return type;
}

You could also use commonsware merge adapter

https://github.com/commonsguy/cwac-merge

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Judging condition is wrong

convertView aways be null.

you can search android ListView resusing views e.g ListView reusing views when ... I don't want it to

so if you want use two layout, use other Judging condition.

Community
  • 1
  • 1
林知易
  • 1
  • 2