Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在更新listview中的多行_Android_Listview_View - Fatal编程技术网

Android 正在更新listview中的多行

Android 正在更新listview中的多行,android,listview,view,Android,Listview,View,我有一个列表视图,它由名称、文本和图像视图组成。如果名为“John”的用户单击名为“Bob”的行中的imageview,则所有名为“Bob”的行(包括当前单击的行)都应将其imageview更改为另一个图像。我正在尝试使用以下代码执行此操作: private class MyListAdapter extends ArrayAdapter<CommentInfo> { public MyListAdapter() { super(getActivi

我有一个列表视图,它由名称、文本和图像视图组成。如果名为“John”的用户单击名为“Bob”的行中的imageview,则所有名为“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;
    }
}
私有类MyListAdapter扩展了ArrayAdapter{
公共MyListAdapter()
{
super(getActivity(),R.layout.listview_xml,myComments);
}
@凌驾
公共视图getView(最终整型位置、最终视图转换视图、最终视图组父视图)
{
itemView=convertView;
Bundle=新Bundle();
如果(itemView==null)
{
itemView=LayoutFlater.inflate(R.layout.listview_xml,父项,false);
}
final CommentInfo currentComment=myComments.get(位置);
列表视图列表;
如果(!ht.containsKey(currentComment.userName)){
viewList=newarraylist();
添加(项目视图);
Log.d(“用户名”,currentComment.username);
Log.d(“位置”,整数.toString(位置));
ht.put(currentComment.userName,viewList);
}
否则{
((List)ht.get(currentComment.userName)).add(itemView);
Log.d(“用户名”,currentComment.username);
Log.d(“位置”,整数.toString(位置));
}
最终的ImageView如下=(ImageView)itemView.findViewById(R.id.followUserBtn);
follows.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(跟随_标志==0){
跟随_标志=1;
followed_person=currentComment.userName;
follows.setImageResource(R.drawable.followusersuccesssbtn);
List viewList1=(List)ht.get(currentComment.userName);
对于(视图:viewList1){
ImageView跟随_other=(ImageView)view.findViewById(R.id.followUserBtn);
跟随其他.setImageResource(R.drawable.followusersuccesssbtn);
}
newstorefollowed().execute();
}
否则{
跟随_标志=0;
followed_person=currentComment.userName;
follows.setImageResource(R.drawable.followusericon);
List viewList1=(List)ht.get(currentComment.userName);
对于(视图:viewList1){
ImageView跟随_other=(ImageView)view.findViewById(R.id.followUserBtn);
跟随其他.setImageResource(R.drawable.followusericon);
}
新建DeleteFollowed().execute();
}
}
});            
返回项目视图;
}
}

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

首先,不要在数据和视图之间保持关联。Android ListView重用视图,这意味着您将有一些视图与多个名称关联。我敢肯定你的问题就在于此

您应该始终尝试只修改模型对象,并让列表视图更新UI

在您的情况下,您可以让所有的
CommentInfo
对象引用一个
用户
,使用不同的
CommentInfo
从同一用户引用
用户
对象。然后,您可以将图像存储在此
用户中
,当您不在时,单击更新用户图像并调用
notifyDataSetChanged()以便您的列表更新其UI

附言:你可能不想看一眼。这与您的问题无关,但这是一个很好的做法,可以提高列表滚动的平滑度