将另一个EditText值插入Android数据库?

时间:2016-02-09 16:26:42

标签: java android sqlite listview

我在下面有以下代码,我相信它会设置我的数据库。一切正常,但它只允许我插入一个编辑文本值,然后显示在列表视图中。

理想情况下,我想在数据库中添加时间和另一个编辑文本字段,任何人都可以帮助我。

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);
}

}

更新UI等的MainActivity,对话框工作正常

@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);
}

自定义列表视图布局

<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"
    />

enter image description here

1 个答案:

答案 0 :(得分:1)

这样做

//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);