android 中的 SQLite 的列表视图中未显示数据
Data not being displayed in a listview from SQLite in android
我已经成功创建了一个数据库并将数据插入到 it.However 中,当检索数据并将其显示在列表视图中时,数据不是 displayed.I 我不确定我的问题在哪里 lies.I 在 S/O 和其他教程中进行了研究,但似乎找不到适合我的方法 code.The 以下是我尝试过的方法:
DBHelper.java 扩展了 SQLiteOpenHelper
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_TITLE = "title";
public static final String CONTACTS_COLUMN_AMOUNT = "amount";
public static final String CONTACTS_COLUMN_DESC = "description";
private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, title text,amount text,description text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact (String title, String amount, String description)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("title", title);
contentValues.put("amount", amount);
contentValues.put("description",description);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String title, String amount, String description)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("title", title);
contentValues.put("amount", amount);
contentValues.put("description",description);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllContacts()
{
ArrayList<String> array_list = new ArrayList<>();
hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
System.out.print("List holds:" + array_list);
res.moveToNext();
}
return array_list;
}
}
在 Details.java 中,我在按钮上插入数据的地方单击此方式:
public void run(View view) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
if (mydb.updateContact(id_To_Update, title.getText().toString(), amount.getText().toString(), description.getText().toString())) {
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MyBasket.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Not Updated", Toast.LENGTH_SHORT).show();
}
} else {
if (mydb.insertContact(title.getText().toString(), amount.getText().toString(), description.getText().toString())) {
showAlertDialog();
} else {
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
}
}
}
MyBasket.java 是我显示以这种方式添加的数据的列表视图的地方:
在我的 OnCreate()
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllContacts();
ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
//adding it to the list view.
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
我仍然无法display.I不知道我要去哪里wrong.Any我们将非常感谢您的帮助或建议。
我的Logcat:
03-29 19:35:54.849 3834-3834/com.snappy.stevekamau.cosmeticsapp E/SQLiteLog﹕ (1) table contacts has no column named amount
03-29 19:35:54.949 3834-3834/com.snappy.stevekamau.cosmeticsapp E/SQLiteDatabase﹕ Error inserting amount=Ksh.8.0 title=District 9 description=Action, Sci-Fi, Thriller
android.database.sqlite.SQLiteException: table contacts has no column named amount (code 1): , while compiling: INSERT INTO contacts(amount,title,description) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.snappy.stevekamau.cosmeticsapp.DBHelper.insertContact(DBHelper.java:60)
at com.snappy.stevekamau.cosmeticsapp.Details.run(Details.java:82)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3602)
at android.view.View.performClick(View.java:4100)
at android.view.View$PerformClick.run(View.java:17016)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4838)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
at dalvik.system.NativeStart.main(Native Method)
如果在
处设置断点,array_list的值是多少
ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
System.out.Println("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME));
System.out.Println("List holds:" + array_list);
res.moveToNext();
将其放入您的代码中的 getallcontacts 函数中,并查看输出是否显示数组列表实际被正确填充。
我已经成功创建了一个数据库并将数据插入到 it.However 中,当检索数据并将其显示在列表视图中时,数据不是 displayed.I 我不确定我的问题在哪里 lies.I 在 S/O 和其他教程中进行了研究,但似乎找不到适合我的方法 code.The 以下是我尝试过的方法:
DBHelper.java 扩展了 SQLiteOpenHelper
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_TITLE = "title";
public static final String CONTACTS_COLUMN_AMOUNT = "amount";
public static final String CONTACTS_COLUMN_DESC = "description";
private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, title text,amount text,description text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact (String title, String amount, String description)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("title", title);
contentValues.put("amount", amount);
contentValues.put("description",description);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String title, String amount, String description)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("title", title);
contentValues.put("amount", amount);
contentValues.put("description",description);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllContacts()
{
ArrayList<String> array_list = new ArrayList<>();
hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
System.out.print("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
System.out.print("List holds:" + array_list);
res.moveToNext();
}
return array_list;
}
}
在 Details.java 中,我在按钮上插入数据的地方单击此方式:
public void run(View view) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
if (mydb.updateContact(id_To_Update, title.getText().toString(), amount.getText().toString(), description.getText().toString())) {
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MyBasket.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Not Updated", Toast.LENGTH_SHORT).show();
}
} else {
if (mydb.insertContact(title.getText().toString(), amount.getText().toString(), description.getText().toString())) {
showAlertDialog();
} else {
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
}
}
}
MyBasket.java 是我显示以这种方式添加的数据的列表视图的地方: 在我的 OnCreate()
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllContacts();
ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
//adding it to the list view.
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
我仍然无法display.I不知道我要去哪里wrong.Any我们将非常感谢您的帮助或建议。
我的Logcat:
03-29 19:35:54.849 3834-3834/com.snappy.stevekamau.cosmeticsapp E/SQLiteLog﹕ (1) table contacts has no column named amount
03-29 19:35:54.949 3834-3834/com.snappy.stevekamau.cosmeticsapp E/SQLiteDatabase﹕ Error inserting amount=Ksh.8.0 title=District 9 description=Action, Sci-Fi, Thriller
android.database.sqlite.SQLiteException: table contacts has no column named amount (code 1): , while compiling: INSERT INTO contacts(amount,title,description) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.snappy.stevekamau.cosmeticsapp.DBHelper.insertContact(DBHelper.java:60)
at com.snappy.stevekamau.cosmeticsapp.Details.run(Details.java:82)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3602)
at android.view.View.performClick(View.java:4100)
at android.view.View$PerformClick.run(View.java:17016)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4838)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
at dalvik.system.NativeStart.main(Native Method)
如果在
处设置断点,array_list的值是多少 ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
System.out.Println("database data:" +res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME));
System.out.Println("List holds:" + array_list);
res.moveToNext();
将其放入您的代码中的 getallcontacts 函数中,并查看输出是否显示数组列表实际被正确填充。