-1

我想实现单击项目并转到我们输入数据并保存到数据库中的布局。另外请让我知道数据库更新方法如何更新数据并再次保存到数据库中。我还想要一个长按监听器来显示带有编辑、删除和取消选项的对话框。

  public class Create_Task extends ListActivity{
        Cursor cursor;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_task);

            Button createnote = (Button)findViewById(R.id.createnote);

            createnote.setOnClickListener(new OnClickListener(){


                public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent i = new Intent(getApplicationContext(), Add_Task.class);
            startActivity(i);
                }
            });


            Database_Notepad getnotelist = new Database_Notepad(Create_Task.this);

            Cursor c = getnotelist.Get_All_Note();

            Log.i("CURSOR COUNT", c.getCount() + " ");

            String[] from = new String[] {Database_Notepad.Col_File_Name, Database_Notepad.Col_Due_Date};
            int[] to = new int[] {R.id.filename, R.id.duedatetext};

            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.file_row, c, from, to);

            setListAdapter(adapter);

            getnotelist.close();


        getListView().setOnItemClickListener(new OnItemClickListener()
        {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long rowid) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "You have clicked on " + parent.getItemAtPosition(position).toString() + ".",
                    Toast.LENGTH_SHORT).show();




public class Database_Notepad extends SQLiteOpenHelper {

    private static String Databse_Notepad = "Notepad_Databse";
    private static int Database_Version = 1;
    private static String Table_Notepad = "Notepad_table";
    static String Col_ID = "_id";
    static String Col_File_Name = "fileheading";
    static String Col_File_Data = "filedetails";
    static String Col_Due_Date = "due_date";

    //static SQLiteDatabase db;

    public Database_Notepad(Context context) {
        super(context, Databse_Notepad, null, Database_Version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        String create_notepad_table = "CREATE TABLE " 
                                        + Table_Notepad 
                                        + "(" 
                                        + Col_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                                        + Col_File_Name + " TEXT NOT NULL,"
                                        + Col_File_Data + " TEXT NOT NULL,"
                                        + Col_Due_Date + " DATE )";

        db.execSQL(create_notepad_table);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXIST" + Table_Notepad);
        onCreate(db);

    }

    public long Create_Note(String filename, String filedata, String duedate){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(Col_File_Name, filename);
        values.put(Col_File_Data, filedata);
        values.put(Col_Due_Date, duedate);

        return db.insertOrThrow(Table_Notepad, null, values);
    }

    public Cursor fetchNote(long Id) {

        SQLiteDatabase db = this.getWritableDatabase();

        String[] columans = new String[] {Col_ID, Col_File_Name, Col_File_Data, Col_Due_Date};

        Cursor notecursor = db.query(Table_Notepad, columans, Col_ID + "=?", new String[] {String.valueOf(Id)}, null, null, null);

        if (notecursor != null) {
            notecursor.moveToFirst();
        }
            return notecursor;
    }

    public Cursor Get_All_Note(){

        SQLiteDatabase db = this.getWritableDatabase();

        String[] Columans = new String[] {Col_ID, Col_File_Name, Col_File_Data, Col_Due_Date};
        Cursor cursor = db.query(Table_Notepad, Columans, null, null, null, null, null);

        return cursor;
    }

    /*public Cursor updateNote(int id, String filename, String filedata, String duedate){

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Col_File_Name, filename);
        values.put(Col_File_Data, filedata);
        values.put(Col_Due_Date, duedate);

        Cursor cursor = db.update(Table_Notepad, values, duedate, null);

        return cursor;
    }*/

    public boolean DeleteNote(long Rowid) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(Table_Notepad, Col_ID + "=" + Rowid, null) > 0;

    }

请建议

4

1 回答 1

1

您可能需要使用此 http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html 进行长按
并在您的 listActivity 中

setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog dialog ;
            AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
            builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int arg1) {
                    dialog.dismiss();
                }});


                builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {
                        dialog.dismiss();
                    }});

                builder.setNegativeButton("Cencel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {
                        dialog.dismiss();
                    }});
            dialog = builder.create();

            dialog.show();

            return true;
        }
    });

您还需要使用 rawQuerry 更新数据库中的记录

如何在android中使用rawQuery

希望这可以帮助!

于 2012-12-28T09:51:39.963 回答