使用数据库中的alertdialog填充列表视图

时间:2011-12-30 10:42:37

标签: android database listview alertdialog items

我正在尝试创建一个从数据库中填充的列表视图。在数据库中,我使用与列表相同的活动的警告对话框输入数据。我在警告对话框中有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));
}
  }

我问你们每个人是否能说出可能出现的问题。我真诚地验证了所有的代码,但我无法弄明白我写错的地方。如果你能给我一个建议,我会非常感激,或者更好的是,你可以告诉我这是什么问题。谢谢!

P.S。:这是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)

0 个答案:

没有答案