0

It's seems that i have the same problem as this question : here. But my listview already have fill_parent for height. I did a debug, and it stops after 4 rows and it keeps looping the same ones.

Here's my code :

public class NewsAdapter extends BaseAdapter{
List<News> news;
private Context context;
public NewsAdapter(Context context, List<News> news) {
    this.context = context;
    this.news = news;
}
@Override
public int getCount() {
    return news.size();
}

@Override
public Object getItem(int position) {
    return news.get(position);
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.adapter_news, null);
        ViewHolder holder = new ViewHolder();
        holder.tv_title = (TextView) v.findViewById(R.id.tv_adapter_news_title);
        holder.tv_description = (TextView) v.findViewById(R.id.tv_adapter_news_description);
        holder.tv_date = (TextView) v.findViewById(R.id.tv_adapter_news_date);
        //
        holder.siv_picture = (SmartImageView) v.findViewById(R.id.siv_adapter_news_picture);
        //
        holder.view_color = (View) v.findViewById(R.id.v_adapter_news_color);
        ///
        String type = news.get(position).getNewsType();
        String title = news.get(position).getNewsTitle();
        String description = news.get(position).getNewsDescription();
        String picture = news.get(position).getNewsPicture();
        String date = news.get(position).getNewsDate();
        String link = news.get(position).getNewsLink();
        String id = news.get(position).getNewsId();

        if(title != null){
            holder.tv_title.setText(title);
        }
        if(description != null){
            holder.tv_description.setText(description);
        }
        if(picture != null){
            holder.siv_picture.setImageUrl(picture);
        }else{
            holder.siv_picture.setImageUrl("http://aovivo.slbenfica.pt/Portals/3/noticias/Epoca2012_13/Futebol%20Profissional/CampeonatoNacional_2012_13/Benfica_fcPorto/SLB_Futebol_Matic_Festejos_Benfica_FCPorto_13Janeiro2013_V.jpg?w=227");
        }
        if(date != null){
            holder.tv_date.setText(date);
        }
        if(type.equalsIgnoreCase(News.CATEGORY_ARTICLE)){
            holder.view_color.setBackgroundColor(context.getResources().getColor(R.color.red));
            if(date != null){
                holder.tv_date.setTextColor(context.getResources().getColor(R.color.red));
            }

        }else{
            holder.view_color.setBackgroundColor(context.getResources().getColor(R.color.blue));
        }
    }
    System.out.println("getView " + position + " " + convertView);
    return v;
}
static class ViewHolder {
    public SmartImageView siv_picture;
    public TextView tv_title;
    public TextView tv_description;
    public TextView tv_date;
    public View view_color;
}

}

And here's the xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" 

>

<!-- Photo part -->

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="30"
    android:layout_marginTop="@dimen/dim20dp" 
    android:layout_marginBottom="@dimen/dim20dp" 
    >

    <View
        android:id="@+id/v_adapter_news_color"
        android:layout_width="5dp"
        android:layout_height="100dp" 
        android:layout_centerVertical="true"/>

    <com.loopj.android.image.SmartImageView
        android:id="@+id/siv_adapter_news_picture"
        android:layout_width="150dp"
        android:layout_height="100dp" 
        android:layout_centerInParent="true"
        android:scaleType="fitCenter"
        />
</RelativeLayout>

<!-- Text Part -->

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="70"
    android:orientation="vertical" 
    android:layout_marginTop="@dimen/dim20dp"
    android:layout_marginBottom="@dimen/dim20dp"
    >

    <TextView 
        android:id="@+id/tv_adapter_news_date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="date"
        android:layout_marginBottom="@dimen/dim5dp"
        />

    <TextView
        android:id="@+id/tv_adapter_news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title" 
        style="@style/news_text_title"
        android:layout_marginBottom="@dimen/dim15dp"/>

    <TextView
        android:id="@+id/tv_adapter_news_description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/news_text_description"
        android:text="description" />
</LinearLayout>

</LinearLayout>
Community
  • 1
  • 1
Tsunaze
  • 3,204
  • 7
  • 44
  • 81

2 Answers2

1

You getView() implementation does not handle the case where convertView is not null. Make sure you are using setTag() and getTag() to bind the ViewHolder to that view.

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.adapter_news, null);

        ViewHolder holder = new ViewHolder();
        holder.tv_title = (TextView) v.findViewById(R.id.tv_adapter_news_title);
        // etc...

        v.setTag(holder);
    } else {
        holder = v.getTag();
    }

    News item = getItem(position);
    holder.tv_title.setText(item.getNewsTitle());
    // etc...

    return v;
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • i know what the ViewHolder is for but what is the use of tag, because i didn't set the tag and it worked anyway ? – Tsunaze Jun 21 '13 at 08:26
  • The tag is the whole reason `ViewHolder` works. You do this so that when a view is recycled you do not need to call `findViewById()` to get all the child views again -- they're already referenced byt the ViewHolder, so you just call `getTag()` and cast it to a `ViewHolder`. – Karakuri Jun 24 '13 at 14:39
0

the problem is in the getView where you check to see if v == null which is good but what happens if v is NOT null... you dont handle that so its reusing the last view which is why you are seeing multiples

you should be doing this

if(v == null){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.adapter_news, null);
}

//then set the views after you do the check so it is always done
tyczj
  • 71,600
  • 54
  • 194
  • 296