我试图从数据库中获取数据到listview.i必须在我的dbhelper类中使用表。 一个是登录数据表,另一个是数据存储表。从该表中,我将数据提取到listview。请在这里找到我的代码并告诉我这里有什么问题。我的log cat在getAllValues()方法中显示nullpoint异常。请帮我。
public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "sri.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "login";
private static final String TABLE_CREATE ="CREATE TABLE " + TABLE_NAME + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"username TEXT NOT NULL, password TEXT NOT NULL);";
private static final String DB_ADMIN = "INSERT INTO "+TABLE_NAME+"values(1, admin, password);";
public static final String TABLE_NAME_SAVE="saveinfo";
public static final String KEY_ID="_id";
public static final String KEY_URL="surl";
public static final String KEY_UID="suid";
public static final String KEY_PASS="spassword";
public static final String SCRIPT="CREATE TABLE" + TABLE_NAME_SAVE + "(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"surl TEXT NOT NULL, suid TEXT NOT NULL, spassword TEXT NOT NULL);";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
//Create Database
db.execSQL(TABLE_CREATE);
//create admin account
db.execSQL(DB_ADMIN);
//System.out.println("In onCreate");
db.execSQL(SCRIPT);
}catch(Exception e){
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
SQLiteDatabase db;
public Cursor getAllValues() {
// TODO Auto-generated method stub
String[] clumns={KEY_ID,KEY_URL,KEY_UID,KEY_PASS};
return db.query(TABLE_NAME_SAVE, clumns, null, null, null, null, null);
}
void deleteAllRecords(){
db.delete(TABLE_NAME_SAVE, null, null);
}
void deleteOneRecord(String rowid){
db.delete(TABLE_NAME_SAVE, rowid +"="+KEY_ID, null);
}
public void openDatabase() {
// TODO Auto-generated method stub
}
}
这是我的 DbHelper calss
这是我的 DisplayActivity .java calss
public class DisplayActivity extends Activity implements OnItemLongClickListener {
DbHelper db=new DbHelper(this);
Cursor cursor;
ListView list;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
db.openDatabase();
list = (ListView) findViewById(R.id.lvf);
//updateListView();
list.setOnItemLongClickListener(this);
}
class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
view = getLayoutInflater().inflate(R.layout.details, null);
TextView t0 = (TextView) view.findViewById(R.id.did);
TextView t1 = (TextView) view.findViewById(R.id.durl);
TextView t2 = (TextView) view.findViewById(R.id.duid);
TextView t3 = (TextView) view.findViewById(R.id.dpass);
cursor.moveToPosition(position);
String rowid = cursor.getString(0);
String url = cursor.getString(1);
String uid = cursor.getString(2);
String pass=cursor.getString(3);
t0.setText(rowid);
t1.setText(url);
t2.setText(uid);
t3.setText(pass);
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display, menu);
return true;
}
void updateListView() {
// TODO Auto-generated method stub
cursor = db.getAllValues();
CustomAdapter cs = new CustomAdapter();
list.setAdapter(cs);
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
this.position=pos;
return false;
}
}
答案 0 :(得分:0)
使用游标适配器参见下面的示例:
public class ContactCursorAdapterCT extends CursorAdapter {
public ContactCursorAdapterCT(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
while (cursor.moveToNext()) {
TextView name = (TextView)view.findViewById(R.id.blacklistDB1);
name.setText(cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
TextView phone = (TextView)view.findViewById(R.id.blacklistDB2);
phone.setText(cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.lv, parent, false);
bindView(v, context, cursor);
return v;
}
答案 1 :(得分:0)
您未在db
方法中初始化DbHelper
。从类字段中删除它,那里不需要它。您应该按照getAllValues()
方法启用它,如下所示:
public Cursor getAllValues() {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
String[] clumns={KEY_ID,KEY_URL,KEY_UID,KEY_PASS};
return db.query(TABLE_NAME_SAVE, clumns, null, null, null, null, null);
}
顺便说一下,最好为SQLiteOpenHelper
使用单身人士。在这种情况下,您可以通过`SQLiteDatabase db = getWritableDatabase(); 在所有方法中获取数据库