0

Update As suggested i have added data view holder into my code but still i am facing same problem. I have edited my question and my code please take a look at that.

I want to display 2 view in my List view according to row number.I want to display right hand train engine on 1st row and left hand train engine on 2nd row and vice versa.

To have vice versa row view change I put if condition and check position by mod%2==0 than even otherwise odd.. but when i launch app i get exact left right left right view But when i scroll down i got two right hand train engine row one after another.

Here is Code

public LazyAdapter(Activity a, ArrayList<String> songsList) {
    activity = a;
    data=songsList;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
    Log.d("Listview","Count is"+data.size());
    return data.size();
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    Log.d("position is - ", "position is "+position);
    if(position%2==0)
    {
    return 0;
    }
    else
    {
    return 1;
    }

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    int type = getItemViewType(position);
    System.out.println("getView " + position + " " + convertView + " type = " + type);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
            case 0:
                convertView = inflater.inflate(R.layout.list_row1, null);
                holder.caption = holder.caption = ((ImageView)convertView.findViewById(R.id.train).findViewById(R.id.boogi1).findViewById(R.id.imageView1));
                break;
            case 1:
                convertView = inflater.inflate(R.layout.list_row2, null);
                holder.caption = holder.caption = ((ImageView)convertView.findViewById(R.id.train2).findViewById(R.id.boogi2).findViewById(R.id.imageView1)); 
                break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    return convertView;
}

static class ViewHolder {
    ImageView caption;

    }

List_row1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
 <include android:id="@+id/mainview" layout="@layout/activity_main" />

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <include android:id="@+id/train" layout="@layout/train" />
</HorizontalScrollView>

train.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
    <include android:id="@+id/boogi1" layout="@layout/boogi" />
    <include android:id="@+id/boogi2" layout="@layout/boogi" />
     <include android:id="@+id/engine" layout="@layout/engine" />

In Train2.xml Include tag order is changed.

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77
  • 1
    How `list_row2` and `list_row1` are different other than different background? – Paresh Mayani Apr 15 '13 at 13:43
  • yes in list_row 1 having 2 include tag and than one image view of train engine and in list_row2 having first train engine image view and after that 2 include tag in linear layout but orientation is horizontal.. – Swap-IOS-Android Apr 15 '13 at 13:46

3 Answers3

1

you are not using View Holder to Hold the View because when we scroll the list holder should be there to hold the inflated View....

After inflating your view initialize View holder and then do your stuff in it.

Mahaveer Muttha
  • 1,727
  • 4
  • 21
  • 33
0

if(convertView==null) might be causing you problems here.

Currently, your code that checks which view to use is only checked when convertView = null. In your case the convertView is probably not null, hence it reusing the same view several times until convertView is null again (possibly because of scrolling).

Try leaving that part out and trying again.

Sander van't Veer
  • 5,930
  • 5
  • 35
  • 50
0

as mahaveermuttha and paresh mayani suggested to use viewholder and adilsoomra point out that i forgot to add getViewTypeCount(); and it solve my problem

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77