[英]Inserting another EditText Value into a Database for Android?
I have the following code below which I believe sets up my database. 我有下面的代码,我相信下面的代码建立了我的数据库。 Everything works as it should but it only allows me to insert one edit text value and then be displayed in the list view. 一切正常,但只允许我插入一个编辑文本值,然后在列表视图中显示。
Ideally I would like to add time and another edit text field into the database can anyone please help me. 理想情况下,我想添加时间,并在数据库中添加另一个编辑文本字段,任何人都可以帮助我。
SQLiteOpenHelper SQLiteOpenHelper
public class TaskDBHelper extends SQLiteOpenHelper {
public TaskDBHelper(Context context) {
super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
String sqlQuery =
String.format("CREATE TABLE %s (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"%s TEXT)", TaskContract.TABLE,
TaskContract.Columns.TASK);
Log.d("TaskDBHelper", "Query to form table: " + sqlQuery);
sqlDB.execSQL(sqlQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i2) {
sqlDB.execSQL("DROP TABLE IF EXISTS "+TaskContract.TABLE);
onCreate(sqlDB);
}
} }
MainActivity which updates UI etc, dialogue box works fine MainActivity更新UI等,对话框工作正常
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meds2);
updateUI();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(Meds2.this); //start builder
LayoutInflater inflater = Meds2.this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.alert_box, null);
builder.setView(dialogView);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
final EditText edt2 = (EditText) dialogView.findViewById(R.id.edit2);
builder.setTitle("Add a task");
builder.setMessage("What do you want to do?");
// final EditText inputField = new EditText(Meds2.this);
// final EditText inputField2 = new EditText(Meds2.this);
// builder.setView(inputField);
// builder.setView(inputField2);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String task = edt.getText().toString(); //retrieve vales from fields
String task2 = edt2.getText().toString();
TaskDBHelper helper = new TaskDBHelper(Meds2.this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.clear();
values.put(TaskContract.Columns.TASK, task); //place values in db
values.put(TaskContract.Columns.TASK, task2);
db.insertWithOnConflict(TaskContract.TABLE, null, values,
SQLiteDatabase.CONFLICT_IGNORE);
updateUI(); //refresh list view
}
});
builder.setNegativeButton("Cancel", null);
builder.create().show();
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
SQLiteDatabase sqlDB = new TaskDBHelper(this).getWritableDatabase();
Cursor cursor = sqlDB.query(TaskContract.TABLE,
new String[]{TaskContract.Columns.TASK},
null,null,null,null,null);
cursor.moveToFirst();
while(cursor.moveToNext()) {
Log.d("MainActivity cursor",
cursor.getString(
cursor.getColumnIndexOrThrow(
TaskContract.Columns.TASK)));
}
}
private ListAdapter listAdapter;
public void onDoneButtonClick(View view) {
View v = (View) view.getParent();
TextView taskTextView = (TextView) v.findViewById(R.id.taskTextView);
String task = taskTextView.getText().toString();
String sql = String.format("DELETE FROM %s WHERE %s = '%s'",
TaskContract.TABLE,
TaskContract.Columns.TASK,
task);
helper = new TaskDBHelper(Meds2.this);
SQLiteDatabase sqlDB = helper.getWritableDatabase();
sqlDB.execSQL(sql);
updateUI();
}
private void updateUI() {
helper = new TaskDBHelper(Meds2.this);
SQLiteDatabase sqlDB = helper.getReadableDatabase();
Cursor cursor = sqlDB.query(TaskContract.TABLE,
new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
null, null, null, null, null);
listAdapter = new SimpleCursorAdapter(
this,
R.layout.task_view,
cursor,
new String[]{TaskContract.Columns.TASK},
new int[]{R.id.taskTextView},
0
);
this.setListAdapter(listAdapter);
}
Custom Listview layout 自定义Listview布局
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/taskTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="20dp"
android:layout_toLeftOf="@+id/doneButton"
android:layout_alignBottom="@+id/doneButton"
android:gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/doneButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onDoneButtonClick"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/time"
android:textSize="20dp"
android:layout_marginLeft="94dp"
android:layout_marginStart="94dp"
android:layout_alignBottom="@+id/taskTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toRightOf="@+id/doneButton"
android:gravity="center_vertical"
/>
do this 做这个
//insert first edittext value
values.put(TaskContract.Columns.TASK, task);
db.insertWithOnConflict(TaskContract.TABLE, null, values,SQLiteDatabase.CONFLICT_IGNORE);
values.clear();
//insert seco edittext value
values.put(TaskContract.Columns.TASK, task2);
db.insertWithOnConflict(TaskContract.TABLE, null, values,SQLiteDatabase.CONFLICT_IGNORE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.