开发者

"basic" listView adapter question

开发者 https://www.devze.com 2023-03-05 02:15 出处:网络
I have a listview where each row is composed of a textview and an imageview. I want to set the visibility of the imageview when the adapter is created. following is the relevant piece of code where i

I have a listview where each row is composed of a textview and an imageview. I want to set the visibility of the imageview when the adapter is created. following is the relevant piece of code where i am trying to just SET the 3rd row's imageview to VISIBLE state (by default, all are set to INVISIBLE in my lis_item_icon_text.xml file that is inflated). Can you pls tell me how to achieve this? 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;
    }


remove condition if (convertView == null) and compare position==2 instead of position==3 (bcoz positioning starts from 0 ) .

so ur code will be :

@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;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消