选中的复选框显示错误

时间:2016-02-27 05:23:48

标签: android listview checkbox

我正在使用基本适配器填充列表视图的屏幕上。列表视图的每一行都包含一个圆形图像视图,文本视图和复选框。单击工具栏上的单个勾选,我显示的是对应于选中按钮的用户但显示错误。我正在实现以下屏幕:

Screenshot

1.Bean_Friends

    public class Bean_Friends {
    private String name, url, extension, friendsID;
    private String friendSelected = "false";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public String getFriendsID() {
        return friendsID;
    }

    public void setFriendsID(String friendsID) {
        this.friendsID = friendsID;
    }

    public String getFriendSelected() {
        return friendSelected;
    }

    public void setFriendSelected(String friendSelected) {
        this.friendSelected = friendSelected;
    }
}

2.Adapter_Friends_Group.java

    public class Adapter_Friends_Group extends BaseAdapter {
    private Context context;
    public List<Bean_Friends> listBeanFriends;
    private LayoutInflater inflater;
    private ApiConfiguration apiConfiguration;
    private Bean_Friends bean_friends;


    public Adapter_Friends_Group(Context context, List<Bean_Friends> listBeanFriends) {
        this.context = context;
        this.listBeanFriends = listBeanFriends;
    }

    @Override
    public int getCount() {
        return listBeanFriends.size();
    }

    @Override
    public Object getItem(int i) {
        return listBeanFriends.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (inflater == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if (view == null) {
            view = inflater.inflate(R.layout.feed_item_friends, null);
        }

        //finding different views
        ImageView pic = (ImageView) view.findViewById(R.id.friendsImage);
        TextView txtName = (TextView) view.findViewById(R.id.nameFriends);
        CheckBox chkFriends = (CheckBox) view.findViewById(R.id.chkFriends);

        bean_friends = listBeanFriends.get(i);
        String name = bean_friends.getName();
        String url = bean_friends.getUrl();
        String extension = bean_friends.getExtension();
        apiConfiguration = new ApiConfiguration();
        String api = apiConfiguration.getApi();
        String absolute_url = api + "/" + url + "." + extension;

        //loading image into ImageView                                                                                                                                            iew
        Picasso.with(context).load(absolute_url).error(R.drawable.default_avatar).into(pic);

        //Setting name in the textview
        txtName.setText(name);

        //If check box is checked,then add true to bean else add false to bean
        if (chkFriends.isChecked()) {
            bean_friends.setFriendSelected("true");
        } else {
            bean_friends.setFriendSelected("false");
        }


        chkFriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox cb = (CheckBox) view;
                if (cb.isChecked()) {
                    bean_friends.setFriendSelected("true");
                    Toast.makeText(context, "Check Box is checked : " + cb.isChecked(), Toast.LENGTH_SHORT).show();
                } else {
                    bean_friends.setFriendSelected("false");
                }
            }
        });


        return view;
    }
}

第3。包含视图的活动代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_new_group, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.createGroup:
            createNewGroup();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}


public void createNewGroup() {
    Toast.makeText(NewGroupActivity.this, "clicked", Toast.LENGTH_SHORT).show();
    List<Bean_Friends> listBeanFriends = new ArrayList<>();
    //Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));

    listBeanFriends = adapter_friends_group.listBeanFriends;
    //  Log.e("Size of adapter_friends", String.valueOf(adapter_friends_group.listBeanFriends.size()));
    Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));
    for (int i = 0; i < listBeanFriends.size(); i++) {
        Log.e("Loop Working", String.valueOf(i));
        Bean_Friends bean_friends = listBeanFriends.get(i);
        String friendID = bean_friends.getFriendsID();
        String friendSelected = bean_friends.getFriendSelected();
        String friendName = bean_friends.getName();
        Log.e("FriendsName", friendName);
        Log.e("FriendID", friendID);
        Log.e("friendSelected", friendSelected);
        if (friendSelected.equals("true")) {
            Toast.makeText(NewGroupActivity.this, friendID, Toast.LENGTH_SHORT).show();
        } else {
            // Toast.makeText(NewGroupActivity.this,"true",Toast.LENGTH_SHORT).show();
        }
    }
}

更新代码:

执行以下更改后解决了问题:

适配器

    public class Adapter_Friends_Group extends BaseAdapter {
    private Context context;
    public List<Bean_Friends> listBeanFriends;
    private LayoutInflater inflater;
    private ApiConfiguration apiConfiguration;
    private Bean_Friends bean_friends;


    public Adapter_Friends_Group(Context context, List<Bean_Friends> listBeanFriends) {
        this.context = context;
        this.listBeanFriends = listBeanFriends;
    }

    private class ViewHolder {
        ImageView imageView;
        TextView txtName;
        CheckBox chkFriend;
    }

    @Override
    public int getCount() {
        return listBeanFriends.size();
    }

    @Override
    public Object getItem(int i) {
        return listBeanFriends.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder = null;
        if (inflater == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if (view == null) {
            view = inflater.inflate(R.layout.feed_item_friends, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) view.findViewById(R.id.friendsImage);
            viewHolder.txtName = (TextView) view.findViewById(R.id.nameFriends);
            viewHolder.chkFriend = (CheckBox) view.findViewById(R.id.chkFriends);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        bean_friends = listBeanFriends.get(i);
        String name = bean_friends.getName();
        String url = bean_friends.getUrl();
        String extension = bean_friends.getExtension();
        apiConfiguration = new ApiConfiguration();
        String api = apiConfiguration.getApi();
        String absolute_url = api + "/" + url + "." + extension;

        //loading image into ImageView                                                                                                                                            iew
        Picasso.with(context).load(absolute_url).error(R.drawable.default_avatar).into(viewHolder.imageView);
        //Setting name in the textview
        viewHolder.txtName.setText(name);
        //Setting boolean isSelected if the Checkbox is checked
        viewHolder.chkFriend.setChecked(bean_friends.isSelected());
        viewHolder.chkFriend.setTag(bean_friends);


        viewHolder.chkFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox cb = (CheckBox) view;
                Bean_Friends bean_friends = (Bean_Friends) cb.getTag();
                Toast.makeText(context, "Clicked on Checkbox: " + bean_friends.getName() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
                bean_friends.setIsSelected(cb.isChecked());
            }
        });
        return view;
    }
}

    public class Bean_Friends {
    private String name, url, extension, friendsID;
    boolean isSelected;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public String getFriendsID() {
        return friendsID;
    }

    public void setFriendsID(String friendsID) {
        this.friendsID = friendsID;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setIsSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }
}

活动内的方法

 public void createNewGroup() {
    StringBuffer responseText = new StringBuffer();
    listBeanFriends = adapter_friends_group.listBeanFriends;
    //  Log.e("Size of adapter_friends", String.valueOf(adapter_friends_group.listBeanFriends.size()));
    Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));
    for (int i = 0; i < listBeanFriends.size(); i++) {
        Log.e("Loop Working", String.valueOf(i));
        Bean_Friends bean_friends = listBeanFriends.get(i);
        String friendID = bean_friends.getFriendsID();
        String friendName = bean_friends.getName();
        Log.e("FriendsName", friendName);
        Log.e("FriendID", friendID);
        Log.e("FriendSelected", String.valueOf(bean_friends.isSelected()));
        if (bean_friends.isSelected()) {
            responseText.append("\n" + bean_friends.getName() + "  " + bean_friends.getFriendsID());
        }
    }
    Toast.makeText(NewGroupActivity.this, responseText, Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:2)

我认为错误在于这一行: -

//If check box is checked,then add true to bean else add false to bean
if (chkFriends.isChecked()) {
    bean_friends.setFriendSelected("true");
} else {
    bean_friends.setFriendSelected("false");
}

在这里设置&#39; setFriendSelected&#39;根据是否检查chkFriends。在列表视图中,视图会被重复使用,如果您已经选中了“A&#39;作为朋友,然后你向下滚动,然后是“A&#39; A&#39;也许可以在&#39; C&#39;并通过此代码&#39; C&#39;现在将被检查为朋友。在这里,您希望根据是否&#39; C&#39;来膨胀您的复选框视图。是你的朋友。试试这个: -

//If check box is checked,then add true to bean else add false to bean
    if (bean_friends.getFriendSelected().equals("true")) {
        chkFriends.setChecked(true);
    } else {
        chkFriends.setChecked(false);
    }

P.S:您可以在此处使用视图持有者模式以获得更好的性能。