即使在应用程序完全关闭后,如何将按钮的状态(例如按钮的可见性)保存在 RecyclerView 项目中?

How can I save the button's state(e.g. button's visibility) inside a RecyclerView item even after the application is entirely closed?

我试图在用户单击该按钮时将按钮的状态保存在 RecyclerView 的某个项目中。单击它时,它的可见性将消失,另一个按钮将可见。如何保存按钮的状态,以便在应用程序完全关闭时,当我再次打开它时,按钮的状态仍然存在?

我尝试为按钮的可见性状态创建一个数据库,但我不知道在哪里放置正确的代码来添加数据并保存它。

onBindViewHolder()RecyclerView class 中,这是我放置按钮点击监听器的地方。

@Override
    public void onBindViewHolder(final MyViewHolder holder, int position){
        MakerAdapter h = makerList.get(position);
        final String macString = h.getHMac();

        holder.rIcon.setImageResource(h.getHIcon());
        holder.rDevice.setText(h.getHDevice());
        holder.rBrand.setText(h.getHBrand());
        holder.rIp.setText(h.getHIp());
        holder.rMac.setText(h.getHMac());
        holder.rDate.setText(h.getHDate());
        holder.rWifi.setText(h.getHWifi());

        holder.rMark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSafeDB = new SafeDB(getApplicationContext(), null,null,1);
                holder.rMark.setVisibility(GONE);
                holder.rUnsafe.setVisibility(VISIBLE);
                mSafeDB.addSafeMaker(macString, holder.rMark.getVisibility());
            }
        });

        holder.rUnsafe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSafeDB = new SafeDB(getApplicationContext(), null,null,1);
                holder.rUnsafe.setVisibility(GONE);
                holder.rMark.setVisibility(VISIBLE);
                mSafeDB.addSafeMaker(macString, holder.rUnsafe.getVisibility());
            }
        });
    }

这些是我针对此 (onBindViewHolder) 方法的导入:

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.facebook.FacebookSdk.getApplicationContext;

这是我的数据库

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SafeDB extends SQLiteOpenHelper {

    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "safedb.db";
    private static final String TABLE_NAME = "marked_safe";
    private static final String COL_ID = "id";
    private static final String COL_MAC = "mac";
    private static final String COL_MARK = "mark";

    //////////Housekeeping START
    public SafeDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
        super(context, DB_NAME, factory, DB_VERSION);
    }
    public SafeDB(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // this.context = context;
    }


    @Override
    public void onCreate(SQLiteDatabase db){
        String query = "CREATE TABLE " + TABLE_NAME + "(" +
                COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COL_MAC + " TEXT, " +
                COL_MARK + " INTEGER " +
                ");";
        db.execSQL(query);
    }

    public void open() throws SQLException {
        close();
        this.getWritableDatabase();
    }

    public void closeDB() {
        SQLiteDatabase db = this.getReadableDatabase();
        if (db != null && db.isOpen())
            db.close();
    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1){
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
//////////Housekeeping END

    public void deleteTable(){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }

    //IF FIRST TIME. THIS WILL BE TRIGGERED
    public void addSafeMaker(String mac, int mark){
        ContentValues values = new ContentValues();
        values.put(COL_MAC, mac);
        values.put(COL_MARK, mark);
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);
    }


    //UPDATE THE Arp
    public void updateMaker(String mac, int mark){
        ContentValues values = new ContentValues();
        values.put(COL_MAC, mac);
        values.put(COL_MARK, mark);
        SQLiteDatabase db = getWritableDatabase();
        db.update(TABLE_NAME, values, "id = 1", null);
    }


    //GET THE MAC
    public String getMac(String x) {
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM "+ TABLE_NAME+" WHERE "+ COL_ID+" = '" + x+"'" + " LIMIT 1;" ;
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        String mac = c.getString(c.getColumnIndex("mac"));
        return mac;
    }

    //GET THE BUTTON VISIBILITY VALUE
    public String getSafeValue(String x) {
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM "+ TABLE_NAME+" WHERE "+ COL_ID+" = '" + x+"'" + " LIMIT 1;" ;
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        String mark = c.getString(c.getColumnIndex("mark"));
        return mark;
    }

    //CHECK IF EMPTY
    public boolean isEmpty() {
        boolean e = true;
        SQLiteDatabase db = getWritableDatabase();
        String count = "SELECT count(*) FROM " + TABLE_NAME;
        Cursor c = db.rawQuery(count, null);
        c.moveToFirst();
        int icount = c.getInt(0);
        e = icount <= 0;
        return e;
    }

    public int getCount(){
        int count = 0;
        SQLiteDatabase db = getWritableDatabase();
        String c = "SELECT count(*) FROM " + TABLE_NAME;
        Cursor x = db.rawQuery(c, null);
        x.moveToFirst();
        count = x.getInt(0);
        return count;
    }
}

数据库是你最好的选择,你应该把代码放在onclicklistiner里面。

如果你想 "toggle" 那么在数据库中保存一个布尔值,如果它是 true 则用户已经点击了该项目,如果它是 false,则用户没有点击该项目。

比在你的 onclicklistiner 中你对布尔值做一个 if 语句。

以下是基于您的代码的工作示例,尽管适配器可能完全不同。制作了一些assumptions/guesswork。所以代码是非常符合原则的代码,需要进行调整。

上面在适配器实例化时添加了一个mac(假设一个mac是唯一的),具体的mac只会添加一次

在 onBindView 方法中,从数据库中检索 status/mark 以确定显示哪个按钮。

单击按钮时,显示的按钮会切换,相应的 mac(按钮的标签用于存储 mac)用于根据 [= 更新相应的行59=].

SafeDB.java

public class SafeDB extends SQLiteOpenHelper {

    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "safedb.db";
    private static final String TABLE_NAME = "marked_safe";
    private static final String COL_ID = "id";
    private static final String COL_MAC = "mac";
    private static final String COL_MARK = "mark";

    //////////Housekeeping START
    public SafeDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
        super(context, DB_NAME, factory, DB_VERSION);
    }
    public SafeDB(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db){
        String query = "CREATE TABLE " + TABLE_NAME + "(" +
                COL_ID + " INTEGER PRIMARY KEY, " + // AUTOINCREMENT REMOVED NOT NECESSARY
                COL_MAC + " TEXT UNIQUE, " + // Probably should be unique
                COL_MARK + " INTEGER " +
                ");";
        db.execSQL(query);
    }

    public void open() throws SQLException {
        close();
        this.getWritableDatabase();
    }

    public void closeDB() {
        SQLiteDatabase db = this.getReadableDatabase();
        if (db != null && db.isOpen())
            db.close();
    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1){
        deleteTable(); // might as well use the deleteTable method as it exists
        onCreate(db);
    }
//////////Housekeeping END

    public void deleteTable(){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }

    //IF FIRST TIME. THIS WILL BE TRIGGERED
    // wont hurt to return the id of the inserted row (-1 if no row inserted)
    public long addSafeMaker(String mac, int mark){
        ContentValues values = new ContentValues();
        values.put(COL_MAC, mac);
        values.put(COL_MARK, mark);
        SQLiteDatabase db = getWritableDatabase();
        return db.insert(TABLE_NAME, null, values);
    }


    //UPDATE THE Arp
    public int updateMaker(String mac, int mark){
        ContentValues values = new ContentValues();
        values.put(COL_MARK, mark);
        //values.put(COL_MARK, mark); // guess this wont change rather that it will be used to determine the row to be updated
        SQLiteDatabase db = getWritableDatabase();
        //db.update(TABLE_NAME, values, "id = 1", null); // Will only ever update 1 specific row
        String whereclause = COL_MAC + "=?";
        String[] whereargs = new String[]{mac};
        return db.update(TABLE_NAME,values,whereclause,whereargs);
    }


    //GET THE MAC
    public String getMac(String x) {
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM "+ TABLE_NAME+" WHERE "+ COL_ID+" = '" + x+"'" + " LIMIT 1;" ;
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        String mac = c.getString(c.getColumnIndex("mac"));
        return mac;
    }

    public boolean getSafeValue(String mac) {
        int mark = 0; // assume marked safe if row not found
        SQLiteDatabase db = this.getWritableDatabase();
        String whereclause = COL_MAC + "=?";
        String[] whereargs = new String[]{mac};
        Cursor c = db.query(TABLE_NAME,null,whereclause,whereargs,null,null,null);
        if (c.moveToFirst()) {
            mark = c.getInt(c.getColumnIndex(COL_MARK));
        }
        return mark < 1;
    }

    //GET THE BUTTON VISIBILITY VALUE
    /*
    public String getSafeValue(String x) {
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM "+ TABLE_NAME+" WHERE "+ COL_ID+" = '" + x+"'" + " LIMIT 1;" ;
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        String mark = c.getString(c.getColumnIndex("mark"));
        return mark;
    }
    */

    //CHECK IF EMPTY
    /*
    public boolean isEmpty() {
        boolean e = true;
        SQLiteDatabase db = getWritableDatabase();
        String count = "SELECT count(*) FROM " + TABLE_NAME;
        Cursor c = db.rawQuery(count, null);
        c.moveToFirst(); // WARINING if no rows then next line will crash INDEX OUT OF BOUNDS
        int icount = c.getInt(0);
        e = icount <= 0;
        return e;
    }
    */

    public long getCount(){
        int count = 0;
        SQLiteDatabase db = getWritableDatabase();
        return DatabaseUtils.queryNumEntries(this.getWritableDatabase(),TABLE_NAME);
        /*
        quick form used as above
        String c = "SELECT count(*) FROM " + TABLE_NAME;
        Cursor x = db.rawQuery(c, null);
        x.moveToFirst();
        count = x.getInt(0);
        return count;
        */
    }
}
  • 您应该注意评论,您的代码存在一些问题。

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    ArrayList<String> mMacList;
    SafeDB mDB;
    Context mContext;


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView mMac;
        public Button mSafe;
        public Button mUnsafe;

        public MyViewHolder(View view) {
            super(view);
            mMac = view.findViewById(R.id.name);
            mSafe = view.findViewById(R.id.marksafe);
            mUnsafe = view.findViewById(R.id.markunsafe);
        }
    }

    public MyAdapter(Context context,ArrayList<String> maclist) {
        mMacList = maclist;
        mContext = context;
        mDB = new SafeDB(context);

        // Could add the mac's to the DB here (note DB changed so mac is unqiue so same mac won't be added)
        for (String mac: maclist) {
            mDB.addSafeMaker(mac,0);
        }
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mylist_item,viewGroup, false);
        MyViewHolder vh = new MyViewHolder(v);
        mContext = viewGroup.getContext();
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder viewHolder, int i) {
        viewHolder.mMac.setText(mMacList.get(i));
        if (mDB == null) {
           mDB = new SafeDB(viewHolder.mMac.getContext());
        }
        //Set the Tag for the buttons with the mac so it can be retrieved
        viewHolder.mSafe.setTag(mMacList.get(i));
        viewHolder.mUnsafe.setTag(mMacList.get(i));

        // Display the buttons according to the database
        if (mDB.getSafeValue(mMacList.get(i))) {
            viewHolder.mSafe.setVisibility(View.GONE);
            viewHolder.mUnsafe.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mSafe.setVisibility(View.VISIBLE);
            viewHolder.mUnsafe.setVisibility(View.GONE);
        }
        // Add the onCLickListeners
        viewHolder.mSafe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.mSafe.setVisibility(View.GONE);
                viewHolder.mUnsafe.setVisibility(View.VISIBLE);
                String mac = (String) ((Button) viewHolder.mSafe).getTag();
                changeSafeMark((String)viewHolder.mSafe.getTag(),0);
            }
        });
        viewHolder.mUnsafe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.mSafe.setVisibility(View.VISIBLE);
                viewHolder.mUnsafe.setVisibility(View.GONE);
                changeSafeMark((String)viewHolder.mUnsafe.getTag(),1);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMacList.size();
    }

    public int changeSafeMark(String mac, int mark) {
        int result = mDB.updateMaker(mac,mark);
        return result;
    }
}
  • 这可能完全不同,只使用了最简单的布局。

mylistitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/marksafe"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Make Safe"
        />
    <Button
        android:id="@+id/markunsafe"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Make Unsafe"
        />
</LinearLayout>

MainActivity.java

用于测试的调用activity:-

public class MainActivity extends AppCompatActivity {

    RecyclerView mList;
    RecyclerView.LayoutManager mLayoutManager;
    MyAdapter mMyAdapter;

    // The underlying data (just a list of strings for the macs)
    ArrayList<String> mymacliist = new ArrayList<>(Arrays.asList("M1","M2","M3"));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mList = this.findViewById(R.id.mylist);
        mLayoutManager = new LinearLayoutManager(this);
        mList.setLayoutManager(mLayoutManager);
        mMyAdapter = new MyAdapter(this,mymacliist);
        mList.setAdapter(mMyAdapter);
    }
}

结果

当第一个 运行 :-

点击M2和M3后(点击时按钮变为Make Safe),App停止再启动:-