本文介绍了从不同的方法更新适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个arrayadapter.I我列出这个项目。

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

我的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);
}

正如你所看到的,我用badge.But徽章装载是不可见的,我想从具体项目另一种方法表现出来。

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.

例如: randomAdapter(position_id).holder.badge.show(); 我要一个code像this.How我可以做到这一点。

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

推荐答案

试试这个(未测试)code:

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
}

更新:这只适用于可见行结果
如果你想改变是不可见的行徽章,你应该考虑到ViewHolders存储在适配器列表,并从那里访问它们。

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.

这篇关于从不同的方法更新适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 19:03