-1

I want to implement click on item and go to layout where we have enter data and saved into the database. Also please do let me know the data base update method how can I update data and again save into the database. Also I want a long touch listener on to show dialog with edit, delete and cancel options.

  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;

    }

Please suggest

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shweta
  • 1,145
  • 5
  • 18
  • 35

1 Answers1

1

You may need to use this http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html for long click
and in your 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;
        }
    });

you also need to use rawQuerry for updating record in database

how to use rawQuery in android

Hope this helps!

Community
  • 1
  • 1
QAMAR
  • 2,684
  • 22
  • 28