Update adapter from different method

送分小仙女□ 提交于 2019-12-11 09:31:36

问题


I have a arrayadapter.I am listing items with this.

My code:

    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 with badge.But badge is not visible,I want to show it from another method for specific item.

Example:randomAdapter(position_id).holder.badge.show(); I want a code like this.How can I do this ?


回答1:


You can get the view with

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

checkout get item

Edit: Sorry about that wrong method




回答2:


Try this (not tested) 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 rows
If you want to change the badge for rows that are not visible you should consider to store the ViewHolders in a list in the adapter and accessing them from there.



来源:https://stackoverflow.com/questions/25632167/update-adapter-from-different-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!