Ahmed Osama Ahmed Osama - 1 month ago 12
Android Question

android listview how to make imageview (list item ) visible at specific position in the listview

i'm facing a problem that i have a listview contains text and imageview , i want to make the imageview appear on the even positions only, how to make it ?

That's what i mean

Thanks a lot ...

public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView==null){
LayoutInflater inflater=activity.getLayoutInflater();
convertView=inflater.inflate(id,null);

}
sora item=items.get(position);
TextView tv_id= (TextView) convertView.findViewById(R.id.sora_id);

//TextView tv_title= (TextView) convertView.findViewById(R.id.topic);
int colorPos = position % colors.length;
final int colorPos2 = position % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
tv_id.setTextColor(colors2[colorPos2]);
tv_id.setTypeface(null, Typeface.BOLD);
tv_id.setText(item.getSora().toString());

final ImageView b= (ImageView) convertView.findViewById(R.id.list_item_btn);

b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

if (position ==2)
{
b.setBackgroundColor(0xAA000000);

}
Toast.makeText(getContext(),"Clicked on: "+position,Toast.LENGTH_LONG).show();
}
});


return convertView;
}

Answer

add this in getView

  final ImageView b= (ImageView)  convertView.findViewById(R.id.list_item_btn);
if(position%2!=0){
    b.setVisibility(View.GONE);
}