gpt4 book ai didi

java - RecyclerView 只在 List 中添加一项,而不是添加所有条目

转载 作者:太空宇宙 更新时间:2023-11-04 10:59:14 25 4
gpt4 key购买 nike

我正在尝试使用存储在sqlitedatabase中的条目来构建一个应用程序,并且mainactivity包含recyclerview,它显示来自数据库的条目。问题是 recyclerview 仅显示已输入的第一个条目。

For example if the first entry is "A" and the second entry is "B", the recyclerview only displays "A" and not "B"

recyclerview 适配器的代码如下:

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);
}
}
}

主要 Activity

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>

任何人都可以帮助我显示存储在数据库中的所有条目,而不是仅一个条目

最佳答案

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

关于java - RecyclerView 只在 List 中添加一项,而不是添加所有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47089500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com