ToggleButton更新了所有ListView项目

问题描述 投票:0回答:1

我有一个ListView,每行包含ToggleButton。那ToggleButton基本上是一个收藏按钮。首先,如果存在特定值,我去检查数据库,如果是,我将按钮显示为红色,否则为灰色。问题是每当我点击任何按钮时,所有按钮都会变为红色,反之亦然,默认情况下,滚动所有按钮也会更新。我的适配器代码是:

public class PostAdapter extends ArrayAdapter<PostModel> {
    Drawable mDrawable;
    ViewHolder viewHolder;

    public PostAdapter(Context context, ArrayList<PostModel> objects) {
        super(context, 0, objects);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //Get the date item for this position
        final PostModel postModel = getItem(position);
        boolean isClicked = false;

        //Check if the existing view is being reused, otherwise inflate a new view from custom_row layout
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.custom_row, parent, false);
            viewHolder = new ViewHolder();
            //Casting of views from the custom_row layout
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            viewHolder.tvPost = (TextView) convertView.findViewById(R.id.tvPost);
            viewHolder.imageIcon = (ImageView) convertView.findViewById(R.id.imageIcon);
            viewHolder.toggleButton = (ToggleButton) convertView.findViewById(R.id.myHeartToggleButton);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //Filling each views values
        viewHolder.tvTitle.setText(postModel.getTitle());
        viewHolder.tvPost.setText(postModel.getPost());
        viewHolder.imageIcon.setImageResource(postModel.getAssociatedDrawable());

        final DBHelper dbHelper = new DBHelper(getContext().getApplicationContext());
        dbHelper.open();

        if (dbHelper.isClickedPost(postModel.getTitle())) {
            Drawable mDrawable = ContextCompat.getDrawable(getContext().getApplicationContext(), R.drawable.favorite_icon);
            mDrawable.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN));
            viewHolder.toggleButton.setBackgroundDrawable(mDrawable);
            isClicked = true;
        } else {
            viewHolder.toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(getContext().getApplicationContext(), R.drawable.favorite_icon));
        }

        final boolean finalIsClicked = isClicked;

        viewHolder.toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (!finalIsClicked) {
                        Toast.makeText(getContext().getApplicationContext(), "Clicked.", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext().getApplicationContext(), "Clicked again.", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getContext().getApplicationContext(), "Hello...", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView tvTitle, tvPost;
        ImageView imageIcon;
        ToggleButton toggleButton;
    }
}
android android-adapter android-togglebutton
1个回答
0
投票

第一:

你必须把toggleButton放在viewHolder里面。像这样:

static class ViewHolder {
    TextView tvTitle, tvPost;
    ImageView imageIcon;
    ToggleButton toggleButton;
}

然后改变这一行:

toggleButton = (ToggleButton) convertView.findViewById(R.id.myHeartToggleButton);

至:

viewHolder.toggleButton = (ToggleButton) convertView.findViewById(R.id.myHeartToggleButton);

此外,在其他地方,而不是toggleButton使用viewHolder.toggleButton

另外需要注意的是,isClicked也不应该放在适配器中,因为它为每一行保存不同的值。

© www.soinside.com 2019 - 2024. All rights reserved.