我已经阅读了很多关于如何从数据库中填充列表视图的帖子。但我根本无法在我的应用程序上做到这一点!我不知道我做错了什么。我对编程很陌生,所以有很多东西我不明白,但我正在努力学习:)
我在活动中使用此代码将项目添加到数据库中没有任何问题:
String selectedstone = (String) stonespinner.getSelectedItem();
String weight = etaddweight.getText().toString();
db.addStone(new MyStonesDatabase(selectedstone, weight));
在 DatabaseHandler.java 我有这个:
// Adding new stone
void addStone(MyStonesDatabase stone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_STONE, stone.getStone());
values.put(KEY_WEIGHT, stone.getWeight());
// Inserting Row
db.insert(TABLE_MYSTONES, null, values);
db.close(); // Closing database connection
}
我可以看到通过运行以下命令填充了我的数据库:
Log.d("Reading: ", "Reading all items..");
List<MyStonesDatabase> items = db.getAllstones();
for (MyStonesDatabase cn : items) {
String log = "Id: " + cn.getID() + ", Stone: "
+ cn.getStone() + ", Weight: " + cn.getWeight();
// Writing Items to log
Log.d("Name: ", log);
}
但是,当尝试让列表视图显示数据库时,我的应用程序要么崩溃,要么不显示任何内容。在我的 DatabaseHandler.java 我有这个:
// Getting All items from database
public List<MyStonesDatabase> getAllstones() {
List<MyStonesDatabase> stoneList = new ArrayList<MyStonesDatabase>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_MYSTONES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
MyStonesDatabase stone = new MyStonesDatabase();
stone.setID(Integer.parseInt(cursor.getString(0)));
stone.setStone(cursor.getString(1));
stone.setWeight(cursor.getString(2));
// Adding stone to list
stoneList.add(stone);
} while (cursor.moveToNext());
}
// return stonelist
return stone;
}
我不知道应该在我的活动中添加什么来显示列表视图。
public class MyStones extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
有人可以帮帮我吗,我要放弃了呵呵 :) 谢谢!