在android中的sqlite数据库中的listview中显示数据[关闭]
Posted
技术标签:
【中文标题】在android中的sqlite数据库中的listview中显示数据[关闭]【英文标题】:show data in listview from sqlite database in android [closed] 【发布时间】:2015-08-02 20:33:18 【问题描述】:我正在创建一个 listview 活动来从 sqlite 数据库中检索数据 数据库类为..
package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MyDBHandler extends SQLiteOpenHelper
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB.db";
public static final String TABLE_PRODUCTS = "products";
public static final String TABLE_STUDENT = "students";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public static final String COLUMN_QUANTITY = "quantity";
public static final String COLUMN_ROLLNO = "_rollno";
public static final String COLUMN_STUDENTNAME = "studentname";
public static final String COLUMN_CONTACT = "contact";
SQLiteDatabase data=this.getWritableDatabase();
SQLiteDatabase data1 =this.getReadableDatabase();
Product product;
Detail detail;
public MyDBHandler(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
try
// Create table Place
String CREATE_STUDENTS_TABLE = "CREATE TABLE " +
TABLE_STUDENT + "("
+ COLUMN_ROLLNO + " INTEGER PRIMARY KEY," + COLUMN_STUDENTNAME
+ " TEXT," + COLUMN_CONTACT + " INTEGER" + ");";
db.execSQL(CREATE_STUDENTS_TABLE);
// Create table Reminder
String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
TABLE_PRODUCTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_PRODUCTNAME
+ " TEXT," + COLUMN_QUANTITY + " INTEGER" + ");";
db.execSQL(CREATE_PRODUCTS_TABLE);
catch (SQLException se)
Log.v("DatabaseHandler Oncreate SQLException",
Log.getStackTraceString(se));
catch (Exception e)
Log.v("DatabaseHandler Oncreate Exception",
Log.getStackTraceString(e));
Log.d("database","table created");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
try
// Drop table Place
String placeTableDropQuery = "DROP TABLE IF EXISTS " + TABLE_PRODUCTS;
db.execSQL(placeTableDropQuery);
// Drop table Reminder
String ReminderTableDropQuery = "DROP TABLE IF EXISTS "
+ TABLE_STUDENT;
db.execSQL(ReminderTableDropQuery);
// Upgrade database
onCreate(db);
catch (SQLException se)
Log.v("DatabaseHandler onUpgrade SQLException",
Log.getStackTraceString(se));
catch (Exception e)
Log.v("DatabaseHandler onUpgrade Exception",
Log.getStackTraceString(e));
public String addProduct(Product product)
SQLiteDatabase db = this.getWritableDatabase();
try
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.getProductName());
values.put(COLUMN_QUANTITY, product.getQuantity());
db.insert(TABLE_PRODUCTS, null, values);
db.close();
return "Record insert succussfully...";
catch (SQLiteException se)
Log.v("DatabaseHandler insertStudentRecord Exception",
Log.getStackTraceString(se));
return se.getMessage();
catch (Exception e)
Log.v("DatabaseHandler insertStudentRecord Exception",
Log.getStackTraceString(e));
return e.getMessage();
finally
db.close();
public String addProduct(Detail product)
SQLiteDatabase db = this.getWritableDatabase();
try
ContentValues values = new ContentValues();
values.put(COLUMN_STUDENTNAME, product.getName());
values.put(COLUMN_CONTACT, product.getNo());
db.insert(TABLE_STUDENT, null, values);
db.close();
return "Record insert succussfully...";
catch (SQLiteException se)
Log.v("DatabaseHandler insertTeacherRecord Exception",
Log.getStackTraceString(se));
return se.getMessage();
catch (Exception e)
Log.v("DatabaseHandler insertTeacherRecord Exception",
Log.getStackTraceString(e));
return e.getMessage();
finally
db.close();
public Product findProduct(String productname)
String query = "Select * FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " = \"" + productname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Product product = new Product();
if (cursor.moveToFirst())
cursor.moveToFirst();
product.setID(Integer.parseInt(cursor.getString(0)));
product.setProductName(cursor.getString(1));
product.setQuantity(Integer.parseInt(cursor.getString(2)));
cursor.close();
else
product = null;
db.close();
return product;
public Detail findDetail(int studentname)
String query = "Select * FROM " + TABLE_STUDENT + " WHERE " + COLUMN_ROLLNO + " = \"" + studentname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Detail detail = new Detail();
if (cursor.moveToFirst())
cursor.moveToFirst();
detail.setID(Integer.parseInt(cursor.getString(0)));
detail.setName(cursor.getString(1));
detail.setNo(Integer.parseInt(cursor.getString(2)));
cursor.close();
else
detail= null;
db.close();
return detail;
public boolean deleteProduct(String productname)
boolean result = false;
String query = "Select * FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " = \"" + productname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Product product = new Product();
if (cursor.moveToFirst())
product.setID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_PRODUCTS, COLUMN_ID + " = ?",
new String[] String.valueOf(product.getID()) );
cursor.close();
result = true;
db.close();
return result;
public boolean deleteDetail(String studentname)
boolean result = false;
String query = "Select * FROM " + TABLE_STUDENT + " WHERE " + COLUMN_STUDENTNAME + " = \"" + studentname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Detail detail = new Detail();
if (cursor.moveToFirst())
detail.setID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_STUDENT, COLUMN_ROLLNO + " = ?",
new String[] String.valueOf(detail.getID()) );
cursor.close();
result = true;
db.close();
return result;
创建具有按钮的活动并在按钮上单击 mylistactivity 显示 活动为..
package com.example.database;
import java.util.ArrayList;
import com.example.database.R.string;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MyListActivity extends ListActivity
ArrayAdapter<String> adapter;
MyDBHandler db;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
retrive();
setListAdapter(adapter);
db =new MyDBHandler(MyListActivity.this);
private void retrive()
ArrayList<String>namelist= new ArrayList<String>();
int ii;
//ArrayList<String>studentno = new ArrayList<String>();
SQLiteDatabase sd = db.getReadableDatabase();
Cursor cursor = sd.query(db.TABLE_STUDENT, null, null, null, null, null, null);
ii=cursor.getColumnIndex("studentname");
while (cursor.moveToNext())
namelist.add(cursor.getString(ii));
//studentno.add(cursor.getString(1));
ArrayList<String>combimelist=new ArrayList<String>();
for(int i = 0; i<namelist.size();i++)
combimelist.add(namelist.get(i));
adapter = new ArrayAdapter<String>(MyListActivity.this,R.layout.mylist, combimelist);
以及将其显示为的布局
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical"
android:padding="15dp"
android:textSize="25dp"
android:layout_marginBottom="25dp">
</TextView>
它显示错误为..
05-21 05:08:53.823: I/Choreographer(1887): Skipped 34 frames! The application may be doing too much work on its main thread.
05-21 05:08:54.043: D/AndroidRuntime(1887): Shutting down VM
05-21 05:08:54.043: W/dalvikvm(1887): threadid=1: thread exiting with uncaught exception (group=0x41465700)
05-21 05:08:54.363: E/AndroidRuntime(1887): FATAL EXCEPTION: main
05-21 05:08:54.363: E/AndroidRuntime(1887): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.database/com.example.database.MyListActivity: java.lang.NullPointerException
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.os.Looper.loop(Looper.java:137)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-21 05:08:54.363: E/AndroidRuntime(1887): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 05:08:54.363: E/AndroidRuntime(1887): at java.lang.reflect.Method.invoke(Method.java:525)
05-21 05:08:54.363: E/AndroidRuntime(1887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-21 05:08:54.363: E/AndroidRuntime(1887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-21 05:08:54.363: E/AndroidRuntime(1887): at dalvik.system.NativeStart.main(Native Method)
05-21 05:08:54.363: E/AndroidRuntime(1887): Caused by: java.lang.NullPointerException
05-21 05:08:54.363: E/AndroidRuntime(1887): at com.example.database.MyListActivity.retrive(MyListActivity.java:30)
05-21 05:08:54.363: E/AndroidRuntime(1887): at com.example.database.MyListActivity.onCreate(MyListActivity.java:21)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.Activity.performCreate(Activity.java:5133)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-21 05:08:54.363: E/AndroidRuntime(1887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-21 05:08:54.363: E/AndroidRuntime(1887): ... 11 more
【问题讨论】:
您还有问题吗? 是的,它不显示列表视图 您是对的,由于您展示的异常,它不显示列表视图。请确保提供您想要回答的实际问题,以便我们提供进一步帮助。 @MonikaKrailwhat exactly not working
只发布相关部分!!!!
【参考方案1】:
首先初始化db,然后调用retrive()方法:
替换:
retrive();
setListAdapter(adapter);
db =new MyDBHandler(MyListActivity.this);
收件人:
db =new MyDBHandler(MyListActivity.this);
retrive();
setListAdapter(adapter);
【讨论】:
@NehaAgarwal 你不值得投票,因为代码格式错误。 @SargeBorsch 然后编辑它。谢谢以上是关于在android中的sqlite数据库中的listview中显示数据[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
在android中的sqlite数据库中的listview中显示数据[关闭]