将数据添加到Android Studio中的SqLite数据库中的问题,通过对话框实现。

huangapple go评论72阅读模式
英文:

Data Adding Issue to SqLite DB through Dialogue in Android Studio

问题

以下是您提供的Java代码的翻译部分:

public class MyDialogue extends AppCompatDialogFragment {

    private EditText edtkeytitle;
    //MyDbCode db = new MyDbCode(getActivity()); // 但我在这里初始化时遇到问题

    @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, ""); // 在这里我想要插入

                    }
                });

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

如果您有其他问题或需要更多翻译,请告诉我。

英文:
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.

答案1

得分: 1

使用getContext()而不是getActivity()或this... 对我有所帮助。

英文:

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

答案2

得分: 1

使用**getApplicationContext()**在这里会很有帮助。

英文:

it would be helpful too to use

getApplicationContext()

here.

huangapple
  • 本文由 发表于 2023年2月23日 20:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定