为什么数据库只从我的第一个EditText字段中捕获值而不是从我的第二个EditText中捕获值?

时间:2014-06-15 12:18:30

标签: android database sqlite android-sqlite

我想我之前已经问过这个问题,但我没有得到任何答复或答案。好吧,我成功实现了我的数据库,但它只捕获了我的第一个EditText字段(我有两个)并显示在列表中的值?如果你知道,请帮忙。非常感谢你。

package com.example.sgdriverdiary;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

// A helper class to manage database creation and version management. 
public class MySQLiteHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "mydb";
    private static final int DB_VERSION = 1;

    public static final String TABLE_NAME = "record";
    public static final String COL_LITRE = "rLitre";
    public static final String COL_DATE = "rDate";
    public static final String COL_MILE = "rMile";

    private static final String STRING_CREATE = "CREATE TABLE "
        + TABLE_NAME
        + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_DATE+" DATE, "+COL_MILE+" TEXT, "+COL_LITRE+" TEXT);"; 

    public MySQLiteHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(STRING_CREATE);
        ContentValues cv = new ContentValues(3);
        cv.put(COL_MILE, "500");
        //cv.put(COL_LITRE, "6");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cv.put(COL_DATE,  dateFormat.format(new Date()));
        db.insert(TABLE_NAME, null,cv);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }
}

记录Activity

package com.example.sgdriverdiary;


import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Record extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{
    EditText Text1;
    EditText Text2;
    Button Add;
    ListView List;

    MySQLiteHelper mHelper;
    SQLiteDatabase mDb;
    Cursor mCursor;
    SimpleCursorAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_record);
        Text1 = (EditText)findViewById(R.id.mile);
        Text2 = (EditText)findViewById(R.id.litre);
        Add = (Button)findViewById(R.id.add);
        Add.setOnClickListener(this);
        List = (ListView)findViewById(R.id.list);
        List.setOnItemClickListener(this);
        mHelper = new MySQLiteHelper(this);
    }
    public void onResume(){
        super.onResume();
        mDb = mHelper.getWritableDatabase();
        String[] columns = new String[] {"_id", MySQLiteHelper.COL_DATE, MySQLiteHelper.COL_MILE, MySQLiteHelper.COL_LITRE};
        mCursor = mDb.query(MySQLiteHelper.TABLE_NAME, columns, null, null, null, null, null,null);
        String[] headers = new String[] {MySQLiteHelper.COL_DATE, MySQLiteHelper.COL_MILE, MySQLiteHelper.COL_LITRE};
        mAdapter = new  SimpleCursorAdapter(this, android.R.layout.two_line_list_item,mCursor, headers, new int[]{android.R.id.text1, android.R.id.text2});
        List.setAdapter(mAdapter);
        }
    public void onPause(){
        super.onPause();
        mDb.close();
        mCursor.close();
    }


    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        mCursor.moveToPosition(position);
        String rowId = mCursor.getString(0);
        mDb.delete(MySQLiteHelper.TABLE_NAME, "_id = ?", new String[]{rowId});
        mCursor.requery();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues(3);
        cv.put(MySQLiteHelper.COL_MILE, Text1.getText().toString());
        cv.put(MySQLiteHelper.COL_LITRE, Text2.getText().toString());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cv.put(MySQLiteHelper.COL_DATE, dateFormat.format(new Date()));
        mDb.insert(MySQLiteHelper.TABLE_NAME, null,cv);
        mCursor.requery();
        mAdapter.notifyDataSetChanged();
        Text1.setText(null);
        Text2.setText(null);

    }

}

这是我的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sgdriverdiary.Record$PlaceholderFragment"
    android:background="@drawable/layout_border" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

    <EditText
        android:id="@+id/mile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:hint="Enter Mile"
        android:layout_marginTop="180dp"
        android:ems="10" />

    <EditText
        android:id="@+id/litre"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/mile"
        android:layout_below="@+id/mile"
        android:hint="Enter Litre"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/litre"
        android:layout_below="@+id/litre"
        android:layout_marginTop="19dp"
        android:text="Add New Record" />

</RelativeLayout>

0 个答案:

没有答案