Преобразование действия в фрагмент - PullRequest
0 голосов
/ 01 марта 2020

Я должен написать код для создания базы данных в классе Fragment. Не могли бы вы помочь мне перенести код из AppcompatActivity во Fragment?

Это мой код в AppCompatActivity:

public class Input extends AppCompatActivity {
    SQLiteOpenHelper openHelper;
    SQLiteDatabase db;
    Button _submitbtn;
    EditText _fnametxt, _secnametxt, _uin1txt, _distxt, _symptxt, _spectxt;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_input);
        openHelper=new DatabaseHelper(this);
        _submitbtn = (Button) findViewById(R.id.submitbtn);
        _fnametxt = (EditText) findViewById(R.id.fnametxt);
        _secnametxt = (EditText) findViewById(R.id.secnametxt);
        _uin1txt = (EditText) findViewById(R.id.uin1txt);
        _distxt = (EditText) findViewById(R.id.distxt);
        _symptxt = (EditText) findViewById(R.id.symptxt);
        _spectxt = (EditText) findViewById(R.id.spectxt);

        _submitbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db = openHelper.getWritableDatabase();

                String fname=_fnametxt.getText().toString();
                String secname=_secnametxt.getText().toString();
                String specialty=_spectxt.getText().toString();
                String uin=_uin1txt.getText().toString();
                String disease=_distxt.getText().toString();
                String symptoms=_symptxt.getText().toString();
                insertdata(fname, secname, specialty, uin, disease, symptoms);
                Toast.makeText(getApplicationContext(), "input succesfully complete", Toast.LENGTH_LONG).show();
            }
        });
    }
    public void insertdata(String fname, String secname, String specialty, String uin, String disease, String symptoms){
        ContentValues contentValues = new ContentValues ();

        contentValues.put(DatabaseHelper.COL_1, fname);
        contentValues.put(DatabaseHelper.COL_2, secname);
        contentValues.put(DatabaseHelper.COL_3, specialty);
        contentValues.put(DatabaseHelper.COL_4,  uin);
        contentValues.put(DatabaseHelper.COL_5, disease);
        contentValues.put(DatabaseHelper.COL_6, symptoms);
        long id = db.insert(DatabaseHelper.TABLE1, null, contentValues);
    }
}

DatabaseHelper. java:

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME="register.db";
    public static final String TABLE1="inputted";
    public static final String COL_1="doctors_name";
    public static final String COL_2="doctors_secname";
    public static final String COL_3="doctors_specialty";
    public static final String COL_4="patients_uin";
    public static final String COL_5="disease";
    public static final String COL_6="symptoms";

    public DatabaseHelper(@Nullable Input context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String inputted = "CREATE TABLE " + TABLE1 + "(doctors_name TEXT, doctors_secname TEXT,doctors_specialty TEXT,patients_uin INTEGER FOREIGN KEY, disease TEXT,symptoms TEXT)";
        db.execSQL(inputted);
    }

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

И из-за того, что я создал NavigationDrawer, классы java должны быть во Фрагменте, а не в Деятельности AppCompat. Итак, как мне написать код Input. java во InputFragment. java?

InputFragment. java:

public class InputFragment extends Fragment {

    DatabaseHelper db1;
    SQLiteOpenHelper openHelper;
    SQLiteDatabase db;
    Button _submitbtn;
    EditText _fnametxt, _secnametxt, _uin1txt, _distxt, _symptxt, _spectxt;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.fragment_input, container, false);

        _submitbtn = (Button) v.findViewById(R.id.submitbtn);
        _fnametxt = (EditText) v.findViewById(R.id.fnametxt);
        _secnametxt = (EditText) v.findViewById(R.id.secnametxt);
        _uin1txt = (EditText) v.findViewById(R.id.uin1txt);
        _distxt = (EditText) v.findViewById(R.id.distxt);
        _symptxt = (EditText) v.findViewById(R.id.symptxt);
        _spectxt = (EditText) v.findViewById(R.id.spectxt);
    return v;
    }
}
...