I've a problem that should not be weird but I don't know . I had not used Holder in a first time but my last item was the same as the first , with a Holder my last item is ok but not its layout .
I have an ArrayList and Adapter :
listViewNews = (ListView)view.findViewById(R.id.list_news_items);
newAdapter = new NewsListAdapter(getActivity(),
arListNews);
listViewNews.setOnItemClickListener(new viewNewClickListener());
listViewNews.setAdapter(newAdapter);
And in the latter I have a specific layout for the first item in the list and another for the rest.
public class NewsListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Actualite> newsItems;
Typeface custom_font_bold, custom_font_book;
public NewsListAdapter(Context context, ArrayList<Actualite> newsItems){
this.context = context;
this.newsItems = newsItems;
}
@Override
public int getCount() {
return newsItems.size();
}
@Override
public Object getItem(int position) {
return newsItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
NewHolder holder = null;
if (view == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
holder = new NewHolder();
if(position == 0) { // First item
view = mInflater.inflate(R.layout.new_list_item_une, null);
holder.resume = (TextView) view.findViewById(R.id.resume);
} else { // The rest
view = mInflater.inflate(R.layout.new_list_item, null);
}
holder.imageActu = (ImageView) view.findViewById(R.id.imageActu);
holder.txtTitle = (TextView) view.findViewById(R.id.titreActu);
holder.vw = (View) view.findViewById(R.id.colorActu);
holder.thematique = (TextView) view.findViewById(R.id.thematique);
view.setTag(holder);
} else {
holder = (NewHolder) view.getTag();
}
holder.imageActu.setBackgroundColor(context.getResources().getColor(R.color.grey_200_transparent));
holder.txtTitle.setText(newsItems.get(position).getTitre());
holder.vw.setBackgroundColor(Color.rgb(r, g, b));
holder.thematique.setText(th.toUpperCase());
holder.thematique.setTextColor(Color.rgb(r, g, b));
if(position == 0) {
holder.resume.setText(Html.fromHtml(newsItems.get(position).getResume()));
holder.resume.setTextColor(context.getResources().getColor(R.color.grey_200_transparent));
}
return view;
}
private static class NewHolder {
ImageView imageActu;
TextView txtTitle;
View vw;
TextView thematique;
TextView resume;
}
Why is the layout of my last item is the same as the first ? From my Log, there is no position for the last item , so it uses the layout of the first element. I also note that for the last item is not within the if :
if (view == null) { }