数据未从 SQLite 加载到 Listview
Posted
技术标签:
【中文标题】数据未从 SQLite 加载到 Listview【英文标题】:Data is not loaded into a Listview from SQLite 【发布时间】:2019-07-19 21:19:31 【问题描述】:我正在使用 android studio 创建一个简单的 crud 系统,但记录已成功添加到数据库中。但是当我尝试在 listview 控件视图中查看记录时,无法查看数据显示任何错误。我附上了下面的代码。我已经给出了 Listview textViewList 的 ID 名称。
我创建的数据库类DatabaseHelper
。
Listuser.java
public class ListUsers extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_users);
DatabaseHandler db = new DatabaseHandler(this);
String[] users = db.getAllUsers();
if (users != null)
for (String us : users)
Log.d("String Array Value", us);
db.close();
// Use your own layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, R.id.textViewList, users);
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "userDB";
// Employee table name
private static final String TABLE_USERS = "users";
private static String USER_ID = "user_id";
private static String NAME = "name";
private static String PHONE = "phone";
private static String ADDRESS = "address";
public DatabaseHandler(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "("
+ USER_ID + " INTEGER PRIMARY KEY,"
+ NAME + " TEXT,"
+ PHONE + " TEXT,"
+ ADDRESS + " TEXT"
+ ")";
db.execSQL(CREATE_USER_TABLE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
// Adding new user
public int addUser(User user)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, user.getName());
values.put(ADDRESS, user.getAddress());
values.put(PHONE, user.getPhone());
// Inserting Row
int id = (int) db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
return id;
//updating a existing user
public int editUser(User user, String position)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, user.getName());
values.put(PHONE, user.getPhone());
values.put(ADDRESS, user.getAddress());
// updating row
return db.update(TABLE_USERS, values, USER_ID + " = ?",
new String[] String.valueOf(position) );
public User getUser(String id)
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS, new String[] USER_ID,
NAME, ADDRESS, PHONE , USER_ID + "=?",
new String[] String.valueOf(id), null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
User user = new User(cursor.getString(1),
cursor.getString(2), cursor.getString(3));
db.close();
// return user
return user;
// LIst all users
public String[] getAllUsers()
ArrayList<String> usersList = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_USERS;
try
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst())
do
String user = cursor.getString(1) + ", " + cursor.getString(2) + ", " + cursor.getString(3);
usersList.add(user);
while (cursor.moveToNext());
String[] users = new String[usersList.size()];
db.close();
return (usersList.toArray(users));
catch(Exception e)
// Log.d("Error in getting users from DB", e.getMessage());
return null;
列表视图
<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_
android:layout_
android:orientation="vertical"
tools:context=".ListUsers">
<ListView
android:id="@+id/textViewList"
android:layout_
android:layout_
tools:layout_editor_absoluteX="19dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
【问题讨论】:
将适配器传递给 ListView 我更新了列表视图 【参考方案1】:在活动类的onCreate()
中初始化并填充适配器,
但是你忘了为ListView
设置适配器,所以添加到最后:
ListView listView = findViewById(R.id.textViewList);
listView.setAdapter(adapter);
【讨论】:
以上是关于数据未从 SQLite 加载到 Listview的主要内容,如果未能解决你的问题,请参考以下文章