简体   繁体   English

android中SQLite的列表视图中未显示数据

[英]Data not being displayed in a listview from SQLite in android

I have successfully created a database and inserted data into it.However when retrieving the data and displaying it in a listview ,the data is not being displayed.I am unsure as to where my problem lies.I have researched in S/O and other tutorials but cannot seem to find what suits my code.The following is what i have tried: 我已经成功创建了一个数据库并将数据插入到数据库中。但是,当检索数据并将其显示在列表视图中时,则不会显示数据。我不确定问题出在哪里。我已经在S / O和其他方面进行了研究教程,但似乎找不到适合我的代码。以下是我尝试过的内容:

DBHelper.java which extends SQLiteOpenHelper 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; } } 

In Details.java where i insert the data on button click this way: 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 is where i display the listview of the data added this way: in my OnCreate() 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); 

Still i am unable to display.I dont know where i am going wrong.Any help or suggestions will be much apprecated. 仍然无法显示。我不知道我要去哪里。任何帮助或建议都将不胜感激。

My Logcat: 我的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$1.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) 

What is the value of array_list if you put a breakpoint at 如果在断点处放置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(); 

put that in your code in the getallcontacts function and see whether the output shows the arraylist actually being populated correctly. 将其放在您的代码中的getallcontacts函数中,然后查看输出是否显示实际正确填充的arraylist。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM