Toggle按钮使用CursorAdapter

时间:2017-08-29 05:28:36

标签: android listview android-cursoradapter

美好的一天,我尝试了所有可能的解决方案,但我不能为我工作。我的问题是当我向下滚动并向上滚动时,我的切换按钮会自动更改。

  

金色星形切换按钮检查:

enter image description here

  

向下滚动:

enter image description here

  

向上滚动,星星变为白星

enter image description here

MyCursorAdapter.java

class MyCursorAdapter extends CursorAdapter {
        Context context;
        Database db;
        int f;
        public ClientCursorAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);
            this.context = context;
            db = new Database(context);
        }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.row_layout,viewGroup,false);
        return v;
    }

    @Override
    public void bindView(final View view, Context context, final Cursor cursor) {
        final TextView title = view.findViewById(R.id.Title);
        final TextView artist = view.findViewById(R.id.Artist);
        ToggleButton music = view.findViewById(R.id.minus_one);
        ToggleButton favorite = view.findViewById(R.id.favorite);
        f = (cursor.getInt(cursor.getColumnIndex("_id")));
        music.setEnabled(false);
        if (cursor.getString(cursor.getColumnIndex("AUDIOFILE")) != null) {
            music.setChecked(true);
        } else {
            music.setChecked(false);
        }
        int ft = cursor.getInt(cursor.getColumnIndex("FAVORITE"));
        if (ft == 1) {
            favorite.setChecked(true);
        } else {
            favorite.setChecked(false);
        }
        title.setText(cursor.getString(cursor.getColumnIndex("TITLE")));
        artist.setText(cursor.getString(cursor.getColumnIndex("ARTIST")));

        favorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
                    db.update_favorite(b,title.getText().toString(),artist.getText().toString());
                    compoundButton.setChecked(true);
                } else {
                    db.update_favorite(b,title.getText().toString(),artist.getText().toString());
                    compoundButton.setChecked(false);
                }

            }
        });

    }


}

我不知道这里发生了什么。我是android的新手。请帮忙。

0 个答案:

没有答案