Adapter update in a different way

I have an arrayadapter. I list items with this.

My code is:

    static class ViewHolder {
        TextView nameView;
        ImageView imageView;
        BadgeView badge;
    }
    public void placeRandomUsers(final String search) {
        randomAdapter = new ArrayAdapter<JsonObject>(this, 0) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.random_bars, null);
                    holder = new ViewHolder();
                    holder.nameView=(TextView)convertView.findViewById(R.id.tweet);
                    holder.badge = new BadgeView(getContext(), holder.nameView);
                    holder.badge.setTextColor(Color.WHITE);
                    holder.badge.setBadgeBackgroundColor(Color.parseColor("#FF0019"));
                    holder.imageView=(ImageView)convertView.findViewById(R.id.image);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                if (position >= getCount() - 3 && search.equals("") == true) {
                    loadRandomUsers("");
                }
                JsonObject user=getItem(position);
                String name=user.get("name").getAsString();
                String image_url="http://dd.com/profile/thumb/"+user.get("photo").getAsString();

                holder.nameView.setText(name);


                Ion.with(holder.imageView)
                .placeholder(R.drawable.twitter)
                .load(image_url);
                holder.badge.setText("1");
                holder.badge.hide();
                return convertView;
            }
        };

        ListView listView = (ListView)findViewById(R.id.list);
        listView.setAdapter(randomAdapter);
}

As you can see, I am loading the icon. But the icon is not displayed, I want to show it with a different method for a specific element.

Example: randomAdapter(position_id).holder.badge.show();I need such a code. How can i do this?

+1
source share
2 answers

Try this (untested) code:

int visiblePosition = position_id - listView.getFirstVisiblePosition();
View rowView = listView.getChildAt(visiblePosition);
if (rowView != null) {
   ViewHolder holder = (ViewHolder) rowView.getTag();
   holder.badge.show();
} else {
   //Sorry the row is not visible
}

UPDATE: this only works for visible lines.
If you want to change the icon for lines that are not visible, you should consider storing ViewHolders in a list in the adapter and accessing them from there.

0
source

You can get an idea using

ViewHolder vh = (ViewHolder) randomAdapter.getItem(position).getTag()
vh.badge.show();

pictures get item

:

0

All Articles