我正在使用android studio创建一个简单的crud系统,但记录已成功添加到数据库中。但是当我尝试在listview控件视图中查看记录时,无法查看数据,但会显示任何错误。我附上下面的代码。我给了ID名称的Listview textViewList。
我创建了数据库类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;
}
}
}
ListView
<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=".ListUsers">
<ListView
android:id="@+id/textViewList"
android:layout_width="357dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="19dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
答案 0 :(得分:0)
在活动类的onCreate()
中,初始化并填充适配器
但是您忘了为ListView
设置适配器,因此请添加到末尾:
ListView listView = findViewById(R.id.textViewList);
listView.setAdapter(adapter);