必须在适配器中读取对象值两次

时间:2015-10-06 07:53:26

标签: android listview android-arrayadapter

我有一个适配器,我必须加载到Aphid-FlipView Api。我必须从object加载值并将其填充到XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/rssImage"
            android:layout_width="90dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/rssTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:text="TextView"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/rssImage2"
            android:layout_width="90dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/rssTitle2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:text="TextView"
            android:textSize="20dp" />
    </LinearLayout>

</LinearLayout>

适配器一次从对象读取一个值,但我必须读取两个对象并将其值传递给布局。我该怎么办?

private List<CNN> objects;
ImageView iv;
private Context context;


public CNNArrayAdapter(Context context, int listview1, List<CNN> result) {
    super(context, listview1, result);
    this.objects=result;
    this.context = context;
    // TODO Auto-generated constructor stub
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
    }
    TextView tt = (TextView) v.findViewById(R.id.rssTitle);
     iv = (ImageView) v.findViewById(R.id.rssImage);
     TextView tt2 = (TextView) v.findViewById(R.id.rssTitle2));
     ImageView iv2 = (ImageView)v.findViewById(R.id.rssImage2);      
    CNN i = objects.get(position);
    CNN i2 = objects.get(position+1);
    if(i!=null)
    {
                if (tt != null){
                    tt.setText(i.getTitle());
                }
                if (iv != null){                    
                    Picasso.with(context).load(i.getImgURL()).into(iv);
                }
            if(i2!=null)
            {
                tt.setText(i2.getTitle());
            }
    }
    return v;
}

此代码会导致ListView重复。希望你明白我的观点。

1 个答案:

答案 0 :(得分:0)

I'm not sure why you need to use two object per item...

However, to do that, do the following.

Fiert you need to be sure, or assume, the datas you want to diplsay are in even number.

Override the getCount method of your adapter.

public int getCount() {
     return objects.size() / 2;
}

Change the following lines in getView method

CNN i = objects.get(position);
CNN i2 = objects.get(position+1);

to

CNN i = objects.get(2 * position);
CNN i2 = objects.get(2 * position + 1);

By doing that, your adapter, will display in line 0, object at position 0 and 1 in your listview; then in line 1 object at position 2 and 3; etc ...