Question

i have a ListView showing news with images, and set when (position == 0) change the layout, and then a normal listview_row for the following news, but this only applies for the 3 items showed on screen, then the position goes back to 0 and changes the view again, any help here?

Top of ListView After some scrolling

Here is my lazyAdapter code,

        vi = convertView;

        Log.d("NOTICIAS", "P0sition: " + position);
        // set Layout for 1rst item
        if (convertView==null && position == 0) {
            vi = inflater.inflate(R.layout.noticias_list_item_first, null);     
        }
        // set layout for the next items
        else if(convertView==null  && position != 0){
            vi = inflater.inflate(R.layout.noticias_list_item, null);
        }

        TextView news_id = (TextView)vi.findViewById(R.id.news_id); // news_id
        TextView news_titulo = (TextView)vi.findViewById(R.id.news_titulo); // news_titulo
        TextView news_desc = (TextView)vi.findViewById(R.id.news_desc); // news_desc
        //TextView news_fecha = (TextView)vi.findViewById(R.id.news_fecha); // news_fecha
        ImageView news_img = (ImageView)vi.findViewById(R.id.news_img); // news_img

        HashMap<String, String> news = new HashMap<String, String>();
        news = data.get(position);

        // Setting all values in listview
        news_id.setText(news.get(NoticiasActivity.TAG_NEWS_ID));
        news_titulo.setText(news.get(NoticiasActivity.TAG_NEWS_TITULO));
        news_desc.setText(news.get(NoticiasActivity.TAG_NEWS_DESC));
        //news_fecha.setText(song.get(NoticiasActivity.TAG_NEWS_FECHA));
        imageLoader.DisplayImage(news.get(NoticiasActivity.TAG_NEWS_IMG), news_img);

            return vi;
Was it helpful?

Solution

ListView recycles list items for performance purposes , so in your case its trying to uses the same view for 0,3,6... index.

You need to Override getViewTypeCount and return 2 since , you have two different layout.

 @Override
public int getViewTypeCount() {
    return 2; // return the view type
}

Also , Override getItemViewType and return a unique type for position 0 and same type for other position like this.

@Override
public int getItemViewType(int position){
    // return a unique number
    if(position==0){
        return 0;
    }
    else {
        return 1;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top