如何在SQLite数据库中显示一行到textview / edittext

时间:2015-03-09 13:56:49

标签: android database sqlite

我对Android很新,并且即将创建一个包含客户的应用程序,然后将其与某些操作相关联。 我目前无法解决的问题:在将客户数据插入数据库后,我想切换视图并显示包含所有字段的最后一个条目。插入的值如title,name也会自动创建id和date等字段。当我从DDMS中提取数据库文件时,我可以看到数据在数据库中,但我不知道如何在所需的视图中检索和显示它们。 我看到了几十个将它传递给列表视图的例子,但我找不到任何符合我需要的例子。也许是因为我不知道如何正确地提出要求。如果是这样,请指出我正确的方向。

我的DatabaseHelper:

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



public class DbHelper  {

public static final String TABLE_CUSTOMERS = "customers";
public static final String COLUMN_CUSTOMER_ID = "_id";
public static final String COLUMN_CUSTOMER_TITLE = "title";
public static final String COLUMN_CUSTOMER_FIRST_NAME = "first_name";
public static final String COLUMN_CUSTOMER_LAST_NAME = "last_name";
public static final String COLUMN_CUSTOMER_DATE = "date";

 private DatabaseHelper mDbHelper;
 private SQLiteDatabase mDb;

private static final String DATABASE_NAME = "myround.db";
private static final int DATABASE_VERSION = 1;

 private final Context mCtx;

 private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
        + TABLE_CUSTOMERS + " (" + COLUMN_CUSTOMER_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_CUSTOMER_TITLE + " VARCHAR, "
        + COLUMN_CUSTOMER_FIRST_NAME + " VARCHAR, " + COLUMN_CUSTOMER_LAST_NAME + " VARCHAR, " + COLUMN_CUSTOMER_DATE + " VARCHAR);";

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_CUSTOMERS);
        onCreate(db);
    }

}

public DbHelper(Context ctx) {
    this.mCtx = ctx;
}

public DbHelper open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    if (mDbHelper != null) {
        mDbHelper.close();
    }
}

public long createCustomer(String Title, String FirstName, String LastName,
        String Date) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(COLUMN_CUSTOMER_TITLE, Title);
    initialValues.put(COLUMN_CUSTOMER_FIRST_NAME, FirstName);
    initialValues.put(COLUMN_CUSTOMER_LAST_NAME, LastName);
    initialValues.put(COLUMN_CUSTOMER_DATE, Date);

    long id = mDb.insert(TABLE_CUSTOMERS, null, initialValues);
    return id;

}


public Cursor displayLastCustomer() {

      Cursor mCursor = mDb.query(TABLE_CUSTOMERS, new String[] {COLUMN_CUSTOMER_ID,
        COLUMN_CUSTOMER_TITLE, COLUMN_CUSTOMER_FIRST_NAME, COLUMN_CUSTOMER_LAST_NAME, COLUMN_CUSTOMER_DATE},
        null, null, null, null, null);

      if (mCursor != null) {
       mCursor.moveToLast();
      }
      return mCursor;
     }


}

我的CustomerHelper:

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import android.widget.Toast;

public class CustomerHelper extends Activity implements OnClickListener {

public static final String TAG = "CustomerHelper";
EditText txtTtitle, txtFirstName, txtLastName,
          editID, editTitle, editFirstName, editLastName, editDate;
Button btn_add_customer_to_db;
DbHelper dbHelper;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_new_customer);
    txtTtitle = (EditText) findViewById(R.id.txt_title);
    txtFirstName = (EditText) findViewById(R.id.txt_first_name);
    txtLastName = (EditText) findViewById(R.id.txt_last_name);
    editID = (EditText) findViewById(R.id.ntxt_customer_id);
    btn_add_customer_to_db = (Button) findViewById(R.id.add_customer_to_db);
    btn_add_customer_to_db.setOnClickListener(this);

    dbHelper = new DbHelper(this);
    dbHelper.open();

}

private String getDateTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
}

public void onClick(View view) {
    if (view == btn_add_customer_to_db) {
        String Title = txtTtitle.getText().toString();
        String FirstName = txtFirstName.getText().toString();
        String LastName = txtLastName.getText().toString();
        String Date = getDateTime();

        long id = dbHelper.createCustomer(Title, FirstName, LastName, Date);
        if (id < 0) {
            Toast.makeText(this, "Error - Unsuccessful", Toast.LENGTH_LONG)
                    .show();
        } else {
            Toast.makeText(this, "Success - Record added",
                    Toast.LENGTH_LONG).show();



        }

    }

}

}

我正在寻找一个解决方案来实现这个视图,最后一个条目是我的CustomerHelperClass,这样我也可以修改或删除这个条目。

对于插入新客户,我有add_new_customer.xml,我想将视图切换到update_customer.xml,显示最后一个条目。它们都有标题,名称等的文本字段

update_customer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="@string/update_customer_xml_title"
    android:textSize="14sp" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" >

    <Button
        android:id="@+id/add_activity_to_customer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="@string/add_activity_to_customer_sm_btn"
        android:textSize="10sp" />

    <Button
        android:id="@+id/go_to_view_customer_management"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="@string/go_to_customer_mgmt"
        android:textSize="10sp" />

    <Button
        android:id="@+id/go_to_main_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="@string/go_to_main_menu"
        android:textSize="10sp" />
</LinearLayout>

<Button
    android:id="@+id/delete_customer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout1"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_marginBottom="5dp"
    android:text="@string/delete_customer" />

<Button
    android:id="@+id/save_customer_changes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/delete_customer"
    android:layout_alignBottom="@+id/delete_customer"
    android:layout_alignRight="@+id/linearLayout1"
    android:text="@string/save_customer_changes" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/save_customer_changes"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="5sp" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/ntxt_customer_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="TextView" />

        <EditText
            android:id="@+id/ntxt_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ntxt_customer_id"
            android:hint="@string/title"
            android:inputType="text"
            android:textAppearance="?android:attr/textAppearance"
            android:textColor="@android:color/black" />

        <EditText
            android:id="@+id/ntxt_first_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ntxt_title"
            android:layout_marginTop="8dp"
            android:hint="@string/first_name"
            android:inputType="text"
            android:textAppearance="?android:attr/textAppearance"
            android:textColor="@android:color/black" />

        <EditText
            android:id="@+id/ntxt_last_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ntxt_first_name"
            android:layout_marginTop="8dp"
            android:hint="@string/last_name"
            android:inputType="text"
            android:textAppearance="?android:attr/textAppearance"
            android:textColor="@android:color/black" />

        <EditText
            android:id="@+id/ntxt_customer_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ntxt_last_name"
            android:layout_marginTop="8dp"
            android:hint="date"
            android:inputType="text"
            android:textAppearance="?android:attr/textAppearance"
            android:textColor="@android:color/black" />


    </RelativeLayout>
</ScrollView>

编辑:

 } else {
            Toast.makeText(this, "Success - Record added",
                    Toast.LENGTH_LONG).show();

            setContentView(R.layout.update_customer);

        }

    }

    DbHelper dbHelperInstance = new DbHelper(this); // Assuming it's running
                                                    // in an Activity
    Cursor cursor = dbHelperInstance.displayLastCustomer();
    if (cursor != null) {
        EditText editTextFirstName = (EditText) findViewById(R.id.ntxt_first_name);
        editTextFirstName.setText(cursor.getString(cursor
                .getColumnIndex(DbHelper.COLUMN_CUSTOMER_FIRST_NAME)));
    }
}
}

1 个答案:

答案 0 :(得分:0)

希望你可以在这个答案的基础上,如果你需要进一步的指导,请随时发表评论。

假设您希望使用客户名填充标识为EditText的{​​{1}}。在致电ntxt_first_name

后,您将编写此代码
displayLastCustomer()

以上代码段尝试在使用DatabaseHelper dbHelperInstance = new DbHelper(this); //Assuming it's running in an Activity Cursor cursor = dbHelperInstance.displayLastCustomer(); if(cursor != null) { EditText editTextFirstName = (EditText) findViewById(R.id.ntxt_first_name); editTextFirstName.setText(cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_CUSTOMER_FIRST_NAME))); } 方法提供的虚增的View层次结构中找到标识为View的{​​{1}}(EditText)(希望您已提供) ntxt_frist_name作为参数。然后它只是将其setContentView(int resourceId)字段设置为它从条目R.layout.update_customer当前指向的值中获取的值(由{{1设置为查询中的最后一个条目)列mText

的方法)

就像我已经写过的那样,我希望你能从这个代码片段中获取如何填充其他视图来从cursor对象中的其他列获取数据。

干杯,祝你好运!

编辑:如果您没有在displayLastCustomer()对象(或从其派生的类的对象)中运行此代码,则需要获取DbHelper.COLUMN_CUSTOMER_FIRST_NAME的{​​{1}} Cursor文件已膨胀。