簡體   English   中英

RecyclerView僅在列表中添加一項,而不是添加所有條目

[英]RecyclerView only adds one item in the List instead of adding all the entries

我正在嘗試構建一個應用程序,其中使用將條目存儲在sqlitedatabase中的sqlitedatabase ,並且mainactivity包含顯示database條目的recyclerview 問題在於, recyclerview僅顯示已輸入的第一個條目。

例如,如果第一個條目是“ A”,第二個條目是“ B”,則recyclerview僅顯示“ A”,而不顯示“ B”

下面給出了recyclerview adapter的代碼:

RecyclerView適配器

public class PasswordRecyclerViewAdapter extends RecyclerView.Adapter<PasswordRecyclerViewAdapter.ViewHolder> {

private Context context;
private List<String> accounts;
private int position;

public PasswordRecyclerViewAdapter() {
}

public PasswordRecyclerViewAdapter(Context context, List<String> accounts) {
    this.context = context;
    this.accounts = accounts;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(context).inflate(R.layout.linear_layout_simple_text, parent, false);
    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.title.setText(accounts.get(position));
    holder.title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String data = accounts.get(position);
            Intent intent=new Intent(context, DetailsActivity.class);
            intent.putExtra("Site",data);
            context.startActivity(intent);
        }
    });
}

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

class ViewHolder extends RecyclerView.ViewHolder {

    TextView title;

    public ViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.textview_title);
    }
}
}

主要活動

public class MainActivity extends AppCompatActivity {

private static final String SHARED_PREFS_NAME="MyPrefs";
private RecyclerView recyclerView;
TextView emptyText;
PasswordRecyclerViewAdapter adapter;
List<String> collection;
List<String> myList=new ArrayList<String>();
PasswordDatabase passwordDatabase;
AdapterView.AdapterContextMenuInfo info;
String s;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());
    setContentView(R.layout.activity_main);
    passwordDatabase = new PasswordDatabase(getApplicationContext());
    myList = getArray();
    collection = new ArrayList<>();
    recyclerView = (RecyclerView) findViewById(R.id.listViewID);
    emptyText = (TextView)findViewById(R.id.text2);
    adapter = new PasswordRecyclerViewAdapter(this, myList);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
    registerForContextMenu(recyclerView);
}
public List<String> getArray(){
    /*SharedPreferences sp=this.getSharedPreferences(SHARED_PREFS_NAME,Activity.MODE_PRIVATE);
    Set<String> set=sp.getStringSet("list",new HashSet<String>());
    return new ArrayList<String>(set);*/
    List accounts=passwordDatabase.getAcc();
    return accounts;
}
}

MainActivity getArray()函數中, passwordDatabase.getAcc()返回存儲在sqlitedatabase中的項目列表。 sqlitedatabase幫助器類的代碼如下:

public final class PasswordDatabase extends SQLiteOpenHelper {

String data1;

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "UserCredentials.db";
public static final String TABLE_NAME = "Credentials";
public static final String COLUMN_PASSWORD = "Password";
public static final String COLUMN_ACCOUNT = "Account";
public static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + TABLE_NAME;
private static final String TABLE_CREATE = "CREATE TABLE "
        + TABLE_NAME + " (" +  COLUMN_ACCOUNT  + " TEXT, " + COLUMN_PASSWORD
        + " TEXT,UNIQUE("+ COLUMN_ACCOUNT + "));";

public PasswordDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion){
    onUpgrade(db, oldVersion, newVersion);
}
public void addCredentials(Context context,String account, String password){
    SQLiteDatabase db = this.getWritableDatabase();
    long newRowId=0;
    Boolean flag=false;
    ContentValues values = new ContentValues();
    values.put(COLUMN_ACCOUNT, account);
    values.put(COLUMN_PASSWORD, password);
        newRowId = db.insert(TABLE_NAME, null, values);
}

public void deleteRow(String account){
    SQLiteDatabase db=this.getWritableDatabase();
    String whereClause=COLUMN_ACCOUNT+"=?";
    String[] whereArgs=new String[] {account};
    db.delete(TABLE_NAME,whereClause,whereArgs);
}

public void modify(String account,String pass){
    SQLiteDatabase db=this.getWritableDatabase();
    String sql="UPDATE "+ TABLE_NAME +" SET " +COLUMN_PASSWORD +" = " +pass + " WHERE "+ COLUMN_ACCOUNT +" = " +  account;
    db.execSQL(sql);

}
public int modifyCredentials(String account,String newPass){

    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(COLUMN_PASSWORD,newPass);
    String whereClause=COLUMN_ACCOUNT + " =?";
    String[] whereArgs=new String[]{account};
    int update=db.update(TABLE_NAME,contentValues,whereClause,whereArgs);
    return update;
}

public void deleteAllCredentials(){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME, null, null);
}

public boolean checkdb(){
    SQLiteDatabase db;
    db=this.getWritableDatabase();
    Cursor cursor=db.rawQuery("SELECT * FROM"+TABLE_NAME,null);
    Boolean rowExists;
    if (cursor.moveToFirst()){
        rowExists=false;

    }
    else {
        rowExists=true;
    }
    return rowExists;
}
public List<String> getAcc(){
    SQLiteDatabase db=this.getReadableDatabase();;
    List<String> collection=new ArrayList<>();
    String acc=null;
    Cursor c=null;
    try{
        String query="SELECT " + COLUMN_ACCOUNT + " FROM " + TABLE_NAME;
        c=db.rawQuery(query,null);
        if(c!=null){
            if(c.moveToFirst()){
                do{
                    acc=c.getString(c.getColumnIndex(COLUMN_ACCOUNT));
                    collection.add(acc);
                }
                while (c.moveToNext());
            }
        }
    }
    finally {
        if(c!=null){
            c.close();
        }
        if(db!=null){
            db.close();
        }
    }
    return collection;
}

public String getData(String data){
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, new String[] { COLUMN_ACCOUNT,COLUMN_PASSWORD
            }, COLUMN_ACCOUNT + " = ?", new String[] { data },
            null, null, null, null);
    if (cursor!=null && cursor.moveToFirst()){
        do{
            data1=cursor.getString(1);
        }while (cursor.moveToNext());
    }
    return data1;
}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.nitsilchar.hp.passwordStorage.activity.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/listViewID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#9fece2"
    android:dividerHeight="0.5dp">

</android.support.v7.widget.RecyclerView>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18sp"
        android:text="@string/main_xml_empty_text"/>

</RelativeLayout>

誰能幫我顯示database存儲的所有條目,而不是僅顯示一個條目

確保您的linear_layout_simple_text布局高度android:layout_height="wrap_content"

像下面的代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM