我有一个列表视图,其中每一行由textview和imageview组成。我想在创建适配器时设置imageview的可见性。以下是相关的一段代码,我试图将第3行的imageview设置为VISIBLE状态(默认情况下,在我的lis_item_icon_text.xml文件中,所有文件都被设置为INVISIBLE)。你能告诉我如何实现这个目标吗? TIA。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text,null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(data_text_array[position]);
holder.icon.setImageBitmap(mIcon);
if(position == 3) {
holder.icon.setVisibility(View.VISIBLE);
}
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
答案 0 :(得分:0)
删除条件if(convertView == null)并比较position == 2而不是position == 3(bcoz定位从0开始)。
所以你的代码是:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView = mInflater.inflate(R.layout.list_item_icon_text,null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
holder.text.setText(data_text_array[position]);
holder.icon.setImageBitmap(mIcon);
if(position == 2) {
holder.icon.setVisibility(View.VISIBLE);
}
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}