使用数据库中的警报对话框填充列表视图
我正在尝试创建一个从数据库填充的列表视图。在数据库中,我使用与列表相同的活动的警报对话框输入数据。我的警报对话框中有 3 个按钮:保存、删除、关闭。我的问题是,当我单击“保存”按钮时,出现错误。
这是我的 Liste.class(列表所在的活动):
private View.OnClickListener onAdd = new View.OnClickListener() {
public void onClick(View v) {
edit_box();
}
};
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
noteId = String.valueOf(id);
edit_box();
}
};
private void edit_box(){
final View addView = getLayoutInflater().inflate(R.layout.add, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(addView);
EditText editNote=(EditText) addView.findViewById(R.id.title);
if(noteId != null)
{
Cursor c = helper.getById(noteId);
c.moveToFirst();
editNote.setText(helper.getNote(c));
}
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
EditText editNote = (EditText) addView.findViewById(R.id.title);
if(noteId==null){
helper.insert(editNote.getText().toString());
noteId = null;
}
dataset_cursor.requery();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton){
}
});
builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if(noteId == null)
{
return;
}
else{
helper.delete(noteId);
noteId=null;
}
dataset_cursor.requery();
}
});
builder.show();
}
class NoteAdapter extends CursorAdapter{
NoteAdapter(Cursor c){
super(Liste.this, c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c) {
NoteHolder holder = (NoteHolder) row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row =inflater.inflate(R.layout.row, parent, false);
NoteHolder holder = new NoteHolder(row);
row.setTag(holder);
return(row);
}}
static class NoteHolder {
private TextView note = null;
NoteHolder(View row) {
note=(TextView)row.findViewById(R.id.note);
}
void populateFrom(Cursor c, DBhelp helper){
note.setText(helper.getNote(c));
}
}
这是我的 DBHelper 类的一部分:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Notes(_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insert(String note){
ContentValues cv = new ContentValues();
cv.put("note", note);
getWritableDatabase().insert("Notes", "note", cv);
}
public void update(String id, String note) {
ContentValues cv = new ContentValues();
String[] args={id};
cv.put("note", note);
getWritableDatabase().update("Notes", cv, "_id=?", args);
}
public void delete(String id){
getWritableDatabase().delete("Notes", "_id=?", new String[] {id});
}
public Cursor getAll(){
return(getReadableDatabase()
.rawQuery("SELECT _id, note FROM Notes", null));
}
public Cursor getById(String id){
String[] args={id};
return(getReadableDatabase()
.rawQuery("SELECT _id, note FROM Notes WHERE _id=?",
args));
}
public String getNote(Cursor c){
return(c.getString(1));
}
}
我问你们每个人是否可以说出可能是什么问题。我真诚地验证了所有代码但我不知道我哪里写错了。如果您能给我建议,或者更好的是,您可以告诉我问题是什么,我将非常感激。谢谢!
PS:这是 logcat :
12-30 12:57:59.079: W/KeyCharacterMap(340): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 12:58:00.359: D/AndroidRuntime(340): Shutting down VM
12-30 12:58:00.359: W/dalvikvm(340): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-30 12:58:00.390: E/AndroidRuntime(340): FATAL EXCEPTION: main
12-30 12:58:00.390: E/AndroidRuntime(340): java.lang.NullPointerException
12-30 12:58:00.390: E/AndroidRuntime(340): at com.project.mapshop.Liste$3.onClick(Liste.java:96)
12-30 12:58:00.390: E/AndroidRuntime(340): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-30 12:58:00.390: E/AndroidRuntime(340): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 12:58:00.390: E/AndroidRuntime(340): at android.os.Looper.loop(Looper.java:123)
12-30 12:58:00.390: E/AndroidRuntime(340): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 12:58:00.390: E/AndroidRuntime(340): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 12:58:00.390: E/AndroidRuntime(340): at java.lang.reflect.Method.invoke(Method.java:521)
12-30 12:58:00.390: E/AndroidRuntime(340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 12:58:00.390: E/AndroidRuntime(340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 12:58:00.390: E/AndroidRuntime(340): at dalvik.system.NativeStart.main(Native Method)
I'm trying to make a listview which is populated from the database.In the database I enter data with an alert dialog from the same activity as the list is. I have 3 buttons in the alert dialog: save , delete, close. My problem is that, when I click the save button it gives me an Error.
Here is my Liste.class (the activity where is the list):
private View.OnClickListener onAdd = new View.OnClickListener() {
public void onClick(View v) {
edit_box();
}
};
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
noteId = String.valueOf(id);
edit_box();
}
};
private void edit_box(){
final View addView = getLayoutInflater().inflate(R.layout.add, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(addView);
EditText editNote=(EditText) addView.findViewById(R.id.title);
if(noteId != null)
{
Cursor c = helper.getById(noteId);
c.moveToFirst();
editNote.setText(helper.getNote(c));
}
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
EditText editNote = (EditText) addView.findViewById(R.id.title);
if(noteId==null){
helper.insert(editNote.getText().toString());
noteId = null;
}
dataset_cursor.requery();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton){
}
});
builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if(noteId == null)
{
return;
}
else{
helper.delete(noteId);
noteId=null;
}
dataset_cursor.requery();
}
});
builder.show();
}
class NoteAdapter extends CursorAdapter{
NoteAdapter(Cursor c){
super(Liste.this, c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c) {
NoteHolder holder = (NoteHolder) row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row =inflater.inflate(R.layout.row, parent, false);
NoteHolder holder = new NoteHolder(row);
row.setTag(holder);
return(row);
}}
static class NoteHolder {
private TextView note = null;
NoteHolder(View row) {
note=(TextView)row.findViewById(R.id.note);
}
void populateFrom(Cursor c, DBhelp helper){
note.setText(helper.getNote(c));
}
}
Here is a part of my DBHelper class:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Notes(_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insert(String note){
ContentValues cv = new ContentValues();
cv.put("note", note);
getWritableDatabase().insert("Notes", "note", cv);
}
public void update(String id, String note) {
ContentValues cv = new ContentValues();
String[] args={id};
cv.put("note", note);
getWritableDatabase().update("Notes", cv, "_id=?", args);
}
public void delete(String id){
getWritableDatabase().delete("Notes", "_id=?", new String[] {id});
}
public Cursor getAll(){
return(getReadableDatabase()
.rawQuery("SELECT _id, note FROM Notes", null));
}
public Cursor getById(String id){
String[] args={id};
return(getReadableDatabase()
.rawQuery("SELECT _id, note FROM Notes WHERE _id=?",
args));
}
public String getNote(Cursor c){
return(c.getString(1));
}
}
I'm asking everyone of you if you could tell what may be the problem.I sincerely verified all the code but I can't figure it out where I wrote wrong. I would be very grateful if you could give me an advice or, even better, you could tell me what is the problem. Thank you !
P.S.: Here is the logcat :
12-30 12:57:59.079: W/KeyCharacterMap(340): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 12:58:00.359: D/AndroidRuntime(340): Shutting down VM
12-30 12:58:00.359: W/dalvikvm(340): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-30 12:58:00.390: E/AndroidRuntime(340): FATAL EXCEPTION: main
12-30 12:58:00.390: E/AndroidRuntime(340): java.lang.NullPointerException
12-30 12:58:00.390: E/AndroidRuntime(340): at com.project.mapshop.Liste$3.onClick(Liste.java:96)
12-30 12:58:00.390: E/AndroidRuntime(340): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-30 12:58:00.390: E/AndroidRuntime(340): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 12:58:00.390: E/AndroidRuntime(340): at android.os.Looper.loop(Looper.java:123)
12-30 12:58:00.390: E/AndroidRuntime(340): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 12:58:00.390: E/AndroidRuntime(340): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 12:58:00.390: E/AndroidRuntime(340): at java.lang.reflect.Method.invoke(Method.java:521)
12-30 12:58:00.390: E/AndroidRuntime(340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 12:58:00.390: E/AndroidRuntime(340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 12:58:00.390: E/AndroidRuntime(340): at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论