RecyclerView仅在列表中添加一个项目,而不是添加所有条目

时间:2017-11-03 06:11:02

标签: java android android-recyclerview android-sqlite

我正在尝试使用条目存储在sqlitedatabase中而mainactivity包含recyclerview来构建应用,其中database显示来自recyclerview的条目。问题是recyclerview仅显示已输入的第一个条目。

  

例如,如果第一个条目是" A"第二个条目是" B",recyclerview adapter仅显示" A"而不是" B"

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); } } } 的代码如下:

RecyclerView适配器

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()

MainActivity中的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; } } 助手类的代码如下:

<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>

activity_main.xml中

database

任何人都可以帮我显示{{1}}中存储的所有条目,而不是仅显示一个条目

1 个答案:

答案 0 :(得分:0)

确保 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">