I am new to SQLite databases and I would really appreciate your help.
I have an alertDialog with an EditText value that I would like to store in the Database after the user clicks on the 'CREATE' positive button. I dont know how to go about storing this value correctly and then moving on to a new activity once the value is now in the database.
When the user clicks on a button on the HomeActivity class, the alertDialog pops up to request a name. When the user clicks the positive button, it should save the name in a database and redirect the user to a class called RecordPathScreenActivity.
If you could look at my code and help me, I would be most grateful. The 3 classes I have included are the HomeActivity, MyDialog and AdapterDatabaseWHIB classes.
MyDialog class
public class MyDialog extends DialogFragment {
// Button yes, no;
Context context;
EditText pathNameValue;
AdapterDatabaseWHIB adapterDatabaseWHIB;
SQLiteDatabase db;
@
Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Context context = getActivity();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View newPathLayout = inflater.inflate(R.layout.whib_activity_new_path, null);
// Call getActivity as we are not inside an Activity. It will get casted to type context
final AlertDialog.Builder pathCreateBuilder = new AlertDialog.Builder(getActivity());
pathCreateBuilder.setView(newPathLayout);
pathCreateBuilder.setTitle("New Path");
Dialog newPathDialog = pathCreateBuilder.create();
pathCreateBuilder.setNegativeButton("Discard", new DialogInterface.OnClickListener() {@
Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Discard was clicked", Toast.LENGTH_SHORT).show();
}
});
pathCreateBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {@
Override
public void onClick(DialogInterface dialog, int which) {
pathNameValue = (EditText) newPathLayout.findViewById(R.id.pathNameValue);
String pathName = pathNameValue.getText().toString();
adapterDatabaseWHIB.insertPathData(pathName);
// adapterDatabaseWHIB = new SQLite
Toast.makeText(getActivity(), "Create was clicked", Toast.LENGTH_SHORT).show();
}
});
return newPathDialog;
}
/* public void addUser (View view) {
String pathName = pathNameValue.getText().toString();
// SQLiteDatabase db = adapterDatabaseWHIB.getWritableDatabase();
}*/
}
AdapterDatabaseWHIB class public class AdapterDatabaseWHIB {
//youtube nerd herd tutorial
DatabaseWHIB helper;
public AdapterDatabaseWHIB(Context context) {
helper=new DatabaseWHIB(context);
}
//Insert path into database
public long insertPathData(String name) {
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseWHIB.NAME, name);
// contentValues.put(DatabaseWHIB.DESCRIPTION, description);
long id=db.insert(DatabaseWHIB.TABLENAME_PATHS, null, contentValues);
return id;
}
class DatabaseWHIB extends SQLiteOpenHelper {
private static final String DATABASE_NAME="whib_database";
private static final String TABLENAME_PATHS="paths_table";
// DROP TABLE
private static final String DROP_TABLE="DROP TABLE IF EXISTS " + TABLENAME_PATHS +";";
//change version number to upgrade database
private static final int DATABASE_VERSION=4;
private static final String ID_PATH="_id";
private static final String NAME="Name";
private static final String DESCRIPTION="Description";
//CREATE TABLE
private static final String CREATE_TABLE_PATHS="CREATE TABLE " + TABLENAME_PATHS +" (" + ID_PATH +" INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME +" VARCHAR(60), " + DESCRIPTION +" VARCHAR(255));";
private Context context;
public DatabaseWHIB(Context context) {
//null lets us remain with the default cursor.
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
Message.message(context, "constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
Message.message(context, "onCreate called");
// CREATE TABLE
db.execSQL(CREATE_TABLE_PATHS);
}
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion) {
Message.message(context, "onUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
}
public void putInformation(AdapterDatabaseWHIB dWHIB,
String name,
String description) {}
}
HomeActivity class // Dialog Fragment public void showDialog(View v) { MyDialog myDialog = new MyDialog(); // Get fragment manager will allow me to manage the dialog internally. String will allow me to find the dialog by tag myDialog.show(getFragmentManager(),
"My Dialog"); }
Aucun commentaire:
Enregistrer un commentaire