在listview中更新多行

时间:2015-06-25 17:19:58

标签: android listview view

我有一个列表视图,其中包含名称,文本和图像视图。如果用户名为" John"点击行中的imageview,用户名为" Bob",所有行名称为" Bob"包含当前单击的行应将其imageview更改为另一个图像。我试图在以下代码中执行此操作:

private class MyListAdapter extends ArrayAdapter<CommentInfo> {


    public MyListAdapter()
    {
        super(getActivity(), R.layout.listview_xml, myComments);
    }
    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent)
    {
        itemView = convertView;
        Bundle bundle = new Bundle();
        if(itemView == null)
        {
            itemView = layoutInflater.inflate(R.layout.listview_xml, parent, false);

        }
        final CommentInfo currentComment = myComments.get(position);
        List<View> viewList;
        if(!ht.containsKey(currentComment.userName)){
            viewList = new ArrayList<View>();
            viewList.add(itemView);
            Log.d("username", currentComment.userName);
            Log.d("position", Integer.toString(position));
            ht.put(currentComment.userName, viewList);
        }
        else{
            ((List<View>)ht.get(currentComment.userName)).add(itemView);
            Log.d("username", currentComment.userName);
            Log.d("position", Integer.toString(position));
        }
        final ImageView follows = (ImageView) itemView.findViewById(R.id.followUserBtn);

        follows.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(follows_flag == 0){
                    follows_flag = 1;
                    followed_person = currentComment.userName;
                    follows.setImageResource(R.drawable.followusersuccesssbtn);
                    List<View> viewList1 = (List<View>) ht.get(currentComment.userName);
                    for(View view : viewList1){
                        ImageView follows_other = (ImageView)view.findViewById(R.id.followUserBtn);
                        follows_other.setImageResource(R.drawable.followusersuccesssbtn);
                    }
                    new StoreFollowed().execute();
                }
                else{
                    follows_flag = 0;
                    followed_person = currentComment.userName;
                    follows.setImageResource(R.drawable.followusericon);
                    List<View> viewList1 = (List<View>) ht.get(currentComment.userName);
                    for(View view : viewList1){
                        ImageView follows_other = (ImageView)view.findViewById(R.id.followUserBtn);
                        follows_other.setImageResource(R.drawable.followusericon);
                    }
                    new DeleteFollowed().execute();
                }
            }
        });            
        return itemView;
    }
}

在上面的代码中,我存储在哈希表中,用于每个名称,视图列表。我的问题是,当John点击名为Bob的行中的imageview时,所有带有imageview的行都会更改其图像而不是仅包含Bob名称的行。我在这做什么错了?如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

First don't keep an association between your data and your view. Android ListView reuses view which means that you will have some views associated each with multiple names. I'm pretty sure your problem comes form this. You should alway try to only modify your Model Objet and let the list view update the UI. In you case You could have All your CommentInfo objet referencing a User with different CommentInfo from same user refrencing the User object. You can then store you image in this User and when you wan't in on click update the user's image and call notifyDataSetChanged(); so that your list update it's UI. PS : you might wan't to take a look at ViewHolder. It has nothing to do with you problem but it's a good practice and would increase the smoothness of your list's scrolling.