适配器未填充RecyclerView

时间:2018-11-21 21:04:42

标签: android android-recyclerview recycler-adapter

嘿,我有一个当前的应用程序,该应用程序从用户那里获取密码信息并将其存储到SQLiteDatabase中。我的问题是我无法使用此数据填充RecyclerView。我可以看到数据库中有条目。

我的适配器的代码。

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.DataHolder> {
    protected ArrayList<PasswordResults> passwords;

    class DataHolder extends RecyclerView.ViewHolder {
        public TextView id_tv;
        public TextView name_tv;
        public TextView website_tv;
        public TextView desc_tv;
        // public Button view_btn; (IGNORE)
        public TextView username_tv;
        public TextView password_tv;

        DataHolder(View v) {
            super(v);
            id_tv = (TextView) v.findViewById(R.id.tv_id);
            name_tv = (TextView) v.findViewById(R.id.tv_name);
            website_tv = (TextView) v.findViewById(R.id.tv_website);
            desc_tv = (TextView) v.findViewById(R.id.tv_desc);

            username_tv = (TextView) v.findViewById(R.id.tv_username);
            password_tv = (TextView) v.findViewById(R.id.tv_password);
        }
    }

        DataAdapter(ArrayList<PasswordResults> inData){
            passwords = inData;
        }

        @Override
        public DataAdapter.DataHolder onCreateViewHolder(ViewGroup parent, int type){
            View v = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.password_rows, parent, false);
            DataHolder passwordHolder = new DataHolder(v);
            return passwordHolder;
        }

        @Override
        public void onBindViewHolder(DataHolder holder, int position){
            PasswordResults passwordResults = passwords.get(position);

            // set values to text views.
            holder.id_tv.setText(String.valueOf(position+1));
            String name = passwordResults.entryName;
            holder.name_tv.setText(name);
            String website = passwordResults.website;
            holder.website_tv.setText(website);
            String desc = passwordResults.description;
            holder.desc_tv.setText(desc);
            String username = passwordResults.username;
            holder.username_tv.setText(username);
            String password = passwordResults.password;
            holder.password_tv.setText(password);

        }

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

这是我数据库类的代码

public class Database extends SQLiteOpenHelper {

    // version, database name, and table name.
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "DB_Passwords";
    protected static final String TABLE_NAME = "passwords";

    public ArrayList<PasswordResults> passwords;

    // creating names for columns.
    public final static String COLUMN_ID = "_id";
    public final static String COLUMN_NAME = "entryName";
    public final static String COLUMN_WEBSITE = "website";
    public final static String COLUMN_USERNAME = "username";
    public final static String COLUMN_PASSWORD = "password";
    public final static String COLUMN_DESC = "description";

    // creating table string.
    public static final String CREATE_TABLE = "create table if not exists "+TABLE_NAME+" ("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
            + COLUMN_NAME +" VARCHAR(100), "
            + COLUMN_WEBSITE +" VARCHAR(100), "
            + COLUMN_USERNAME +" VARCHAR(100), "
            + COLUMN_PASSWORD +" VARCHAR(100), "
            + COLUMN_DESC +" TEXT)";

    // db helper
    public Database(Context c){
        super(c, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // create table when the app is loaded.
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_TABLE);
    }

    // handle upgrades
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // drop and create new.
        db.execSQL("DROP TABLE "+TABLE_NAME);
        onCreate(db);
    }

    public void addPassword(String n, String w, String u, String p, String d) {
        // get database.
        SQLiteDatabase db = getWritableDatabase();

        try {
            // add scores.
            ContentValues values = new ContentValues();
            values.put(COLUMN_NAME, n);
            values.put(COLUMN_WEBSITE, w);
            values.put(COLUMN_USERNAME, u);
            values.put(COLUMN_PASSWORD, p);
            values.put(COLUMN_DESC, d);

            // insert will handle null values. closing.
            db.insert(TABLE_NAME, "", values);
            db.close();
        } catch (Exception e) {
            e.getLocalizedMessage();
        }
    }

    public void removePassword(String id){
        // get database.
        SQLiteDatabase db = getWritableDatabase();

        // delete database then close or print error.
        try {
            db.delete(TABLE_NAME, "id="+id, null);
            db.close();
        } catch(Exception e) {
            e.getLocalizedMessage();
        }
    }

    public void updatePassword(String id, String n, String w, String u, String p, String d) {
        // get database.
        SQLiteDatabase db = getWritableDatabase();

        //update database then close or print error.
        try {
            ContentValues values = new ContentValues();
            values.put(COLUMN_ID, id);
            values.put(COLUMN_NAME, n);
            values.put(COLUMN_WEBSITE, w);
            values.put(COLUMN_USERNAME, u);
            values.put(COLUMN_PASSWORD, p);
            values.put(COLUMN_DESC, d);
            db.update(TABLE_NAME, values, "_id="+id, null);
            db.close();
        } catch (Exception e) {
            e.getLocalizedMessage();
        }
    }

    public ArrayList<PasswordResults> getPasswords() {
        // get database, password array, and select statement.
        passwords = new ArrayList<>();
        String refQuery = "Select "+COLUMN_NAME+", "+COLUMN_WEBSITE+", "+COLUMN_USERNAME+", "+COLUMN_PASSWORD+", "+COLUMN_DESC+" FROM "+TABLE_NAME;
        SQLiteDatabase db = getWritableDatabase();

        // link to database
        Cursor cursor = db.rawQuery(refQuery, null);
        if(cursor.moveToFirst()) {
            do {
                PasswordResults passwordResults = new PasswordResults();
                passwordResults.entryName = cursor.getString(0);
                passwordResults.website = cursor.getString(1);
                passwordResults.username = cursor.getString(2);
                passwordResults.password = cursor.getString(3);
                passwordResults.description = cursor.getString(4);
            } while(cursor.moveToNext());
        }
        db.close();
        return passwords;
    }

    public ArrayList<PasswordResults> getSelectedPassword(String id) {
        // get database, password array, and select statement.
        passwords = new ArrayList<>();
        String refQuery = "Select * from "+TABLE_NAME+" WHERE "+COLUMN_ID+" = "+id;
        SQLiteDatabase db = getWritableDatabase();

        // link to database
        Cursor cursor = db.rawQuery(refQuery, null);
        if(cursor.moveToFirst()) {
            do {
                PasswordResults passwordResults = new PasswordResults();
                passwordResults.id = cursor.getInt(0);
                passwordResults.entryName = cursor.getString(1);
                passwordResults.website = cursor.getString(2);
                passwordResults.username = cursor.getString(3);
                passwordResults.password = cursor.getString(4);
                passwordResults.description = cursor.getString(5);
            } while(cursor.moveToNext());
        }
        db.close();
        return passwords;
    }

}

这是我的班级,负责成绩

public class PasswordResults {
    public int id;
    public String entryName;
    public String website;
    public String username;
    public String password;
    public String description;
}

2 个答案:

答案 0 :(得分:1)

在这两种方法getPasswords()和getSelectedPasswords()中,您将在以下几行上创建一个空的ArrayList:

passwords = new ArrayList<>(); 

,但是您随后返回密码,而没有将passwordResults添加到其中。在getSelectedPasswords()中,以下代码:

if(cursor.moveToFirst()) {
        do {
            PasswordResults passwordResults = new PasswordResults();
            passwordResults.id = cursor.getInt(0);
            passwordResults.entryName = cursor.getString(1);
            passwordResults.website = cursor.getString(2);
            passwordResults.username = cursor.getString(3);
            passwordResults.password = cursor.getString(4);
            passwordResults.description = cursor.getString(5);
        } while(cursor.moveToNext());
    }
    db.close();
    return passwords;

应更改为:

if(cursor.moveToFirst()) {
        do {
            PasswordResults passwordResults = new PasswordResults();
            passwordResults.id = cursor.getInt(0);
            passwordResults.entryName = cursor.getString(1);
            passwordResults.website = cursor.getString(2);
            passwordResults.username = cursor.getString(3);
            passwordResults.password = cursor.getString(4);
            passwordResults.description = cursor.getString(5);
            passwords.add(passwordResults); //This line is missing!
        } while(cursor.moveToNext());
    }
    db.close();
    return passwords;

还必须将相同的内容添加到getPasswords()方法中。

答案 1 :(得分:0)

我没有足够的声誉来发表评论,但这可能会有所帮助。 填充ArrayList之后,您是否致电?

adapter.notifyDataSetChanged();

如果没有,则必须这样做。如果是,请确保您的回收站视图中肯定没有填充数据,并且布局xml文件没有问题。因为通常如果您对recyclerView的布局文件的设计不正确,即使recyclerView及其适配器正在执行,您也将无法清晰地看到它,而从Java代码中来看,它工作得很好。