我创建了一个片段布局。现在,我想将数据添加到数据库中并显示到每个片段中。我使用弹出菜单,每个片段共享。我的问题是单个按钮如何将项目添加到数据库中以获取不同的片段。请帮忙!
这是我的数据库操作
public class Db_operation_helper extends SQLiteOpenHelper {
private static final String Database_name="database_name";
private static final int Database_version=1;
private static final String Monday_query=
"CREATE TABLE "+ UserContract.Time_monday.Mon_tablename+"("+ UserContract.Time_monday.Mon_subject+" TEXT,"+
UserContract.Time_monday.Mon_instructor+" TEXT,"+ UserContract.Time_monday.Mon_timeone+" TEXT,"+
UserContract.Time_monday.Mon_timetwo+" TEXT);";
private static final String Tuesday_query=
"CREATE TABLE "+ UserContract.Time_tuesday.Tue_tablename+"("+ UserContract.Time_tuesday.Tue_subject+" TEXT,"+
UserContract.Time_tuesday.Tue_instructor+" TEXT,"+ UserContract.Time_tuesday.Tue_timeone+" TEXT,"+
UserContract.Time_tuesday.Tue_timetwo+" TEXT);";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Gradebook_Query);
Log.e("Database Operation", "Table Grade Book Created");
db.execSQL(Monday_query);
Log.e("Database Operation", "Table Monday Created");
db.execSQL(Tuesday_query);
public void monday_add(String subject,String instructor,String timeone,String timetwo,SQLiteDatabase db)
{
ContentValues contentValues=new ContentValues();
contentValues.put(UserContract.Time_monday.Mon_subject,subject);
contentValues.put(UserContract.Time_monday.Mon_instructor,instructor);
contentValues.put(UserContract.Time_monday.Mon_timeone,timeone);
contentValues.put(UserContract.Time_monday.Mon_timetwo,timetwo);
db.insert(UserContract.Time_monday.Mon_tablename, null, contentValues);
Log.e("Database Operation", "One row is inserted into Time Table monday");
}
public void tuesday_add(String subject,String instructor,String timeone,String timetwo,SQLiteDatabase db)
{
ContentValues contentValues=new ContentValues();
contentValues.put(UserContract.Time_tuesday.Tue_subject,subject);
contentValues.put(UserContract.Time_tuesday.Tue_instructor,instructor);
contentValues.put(UserContract.Time_tuesday.Tue_timeone,timeone);
contentValues.put(UserContract.Time_tuesday.Tue_timetwo,timetwo);
db.insert(UserContract.Time_tuesday.Tue_tablename, null, contentValues);
Log.e("Database Operation", "One row is inserted into Time Table Tueday");
}
这是合同类
public class UserContract {
public static abstract class Time_monday{
public static final String Mon_subject="mon_subject";
public static final String Mon_instructor="mon_instructor";
public static final String Mon_timeone="mon_timeone";
public static final String Mon_timetwo="mon_timetwo";
public static final String Mon_tablename="mon_tablename";
}
public static abstract class Time_tuesday{
public static final String Tue_subject="tue_subject";
public static final String Tue_instructor="tue_instructor";
public static final String Tue_timeone="tue_timeone";
public static final String Tue_timetwo="tue_timetwo";
public static final String Tue_tablename="tue_tablename";
}}
而且,这是弹出窗口类java
public class Popwindow_timetable extends Activity {
EditText Subject, Instructor, Timeone, Timetwo;
Context context=this;
Db_operation_helper db_operation_helper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .650), (int) (height * .4));
Subject = (EditText) findViewById(R.id.subject);
Instructor = (EditText) findViewById(R.id.instructor);
Timeone = (EditText) findViewById(R.id.timeone);
Timetwo = (EditText) findViewById(R.id.timetwo);
}
public void timetable_add(View view)
{
String subject=Subject.getText().toString();
String instructor=Instructor.getText().toString();
String timeone=Timeone.getText().toString();
String timetwo=Timetwo.getText().toString();
db_operation_helper=new Db_operation_helper(context);
sqLiteDatabase=db_operation_helper.getWritableDatabase();
db_operation_helper.monday_add(subject,instructor,timeone,timetwo,sqLiteDatabase);
Toast.makeText(getBaseContext(),"DATA IS ADDED",Toast.LENGTH_LONG).show();
db_operation_helper.close();
}
}
在这个java文件中,单击按钮时会调用timetable_add()函数。这里只将数据添加到星期一表中。但是,我想分别向片段添加数据。我的问题是,当当单击按钮时,片段将交换到另一个布局,数据输入将被添加到数据库中的另一个表中。但我不知道如何在按钮单击中实现代码,将数据添加到另一个数据库表中,因为按钮由所有片段共享。