skip to Main Content
public class MyDialogue extends AppCompatDialogFragment {

    private EditText edtkeytitle;
    //MyDbCode db = new MyDbCode(getActivity()); //BUT I AM GETTING ISSUE HERE IN INITIALISATION 

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {


        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialogue_layout, null);

        builder.setView(view)
                .setTitle("Create Title")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String key_title = edtkeytitle.getText().toString();
                        //db.insertDataStore(key_title, ""); // HERE I WANT TO INSERT

                    }
                });

        edtkeytitle = view.findViewById(R.id.edtkeytitle);
        return builder.create();
    }
}

In My app I want to do Is after clicking on floating button to get a dialogue box open and take value afterwards on positive click and it get stored in database. But I am not able to iniitlaise my database here

MyDbCode db = new MyDbCode(getActivity());
here instead of getActivity whether if I am using ‘this’ clause again I am getting issue.
Let me show you my MyDbCode.

public class MyDbCode extends SQLiteOpenHelper {

    private static final String Database_Name = "Alone.db";
    private static final int Database_ID = 1;
    private static final String TABLE_NAME = "alonetable";
    private static final String TABLE_Data_NAME = "alonedata";
    private static final String userpass = "Pass";
    private static final String key_title = "key_title";
    private static final String key_data = "key_data";

    public MyDbCode(Context context) {
        super(context, Database_Name, null , Database_ID);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query1 = "CREATE TABLE " + TABLE_NAME + " (" + userpass + " TEXT)";
        String query2 = "CREATE TABLE " + TABLE_Data_NAME + " ("
                + key_title + " TEXT,"
                + key_data + " TEXT)";
        db.execSQL(query1);
        db.execSQL(query2);

        //insertData("pass");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }

    public void insertData (String pass){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(userpass, pass);
        db.insert(TABLE_NAME, null, values);
        db.close();
    }
    public boolean isexist(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor =db.rawQuery("Select * from alonetable", null);
        if(cursor.getCount() > 0) return true;
        else return false;

    }
    public boolean Ismatched(String pass){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor =db.rawQuery("Select pass from alonetable WHERE pass = ?", new String[]{pass});
        if(cursor.getCount() > 0) return true;
        else return false;

    }


    public void update(String oldpass, String newpass){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(userpass,newpass);
        db.update(TABLE_NAME, cv, "pass = ?", new String[]{oldpass});


        db.close();
    }

    public void insertDataStore (String key_titleent , String key_dataent){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(key_title, key_titleent);
        values.put(key_data,key_dataent);
        db.insert(TABLE_Data_NAME, null, values);
        db.close();
    }

    Cursor readAllData () {
        String query = "SELECT * FROM "+ TABLE_Data_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor= null;
        if(db != null){
            cursor = db.rawQuery(query,null);
        }
        return  cursor;
    }

    public void updateData(String Key_titleupd, String key_Dataupd){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(key_data,key_Dataupd);
        db.update(TABLE_NAME, cv, "pass = ?", new String[]{Key_titleupd});


        db.close();
    }

}

I tried using custom dialogue instead of ALertDialogue but i want if dialogue opens then background of that dialogue gets faded or gets non focusable but i am not able to do so. so i prefered this ALertDialogue but here i am getting Db intialisation issue.

2

Answers


  1. Chosen as BEST ANSWER

    Using getContext() instead of getActivity() or this .... helped me.


  2. it would be helpful too to use

    getApplicationContext()

    here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search