如何使用 hashmap 通过数据库检索不同 editText 的值

How to retrieve the value for different editText through database using hashmap

我想从 HashMapEditText 检索数据 dealer's name customer's name 和 Pcs,然后想在同一个 table 更新交货状态和备注。请告诉我如果我做错了什么。

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "android_api";
    // Login table name
    private static final String TABLE_LOGIN = "login";
    private static final String TABLE_TRACK = "track";
    // Login Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_UID = "uid";
    private static final String KEY_CREATED_AT = "created_at";
    // Track Table Columns names
    private static final String KEY_UIDTRACK = "idtrack";
    private static final String KEY_DOCKETNO = "docketno";
    private static final String KEY_DEALERNAME = "dealername";
    private static final String KEY_CUSTOMERNAME = "customername";
    private static final String KEY_PCS = "pcs";
    private static final String KEY_DELIVERY_STATUS = "deliverstatus";
    private static final String KEY_REMARKS = "remarks";
    private static final String KEY_CREATED_AT_TRACK = "created_at_track";
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
                + KEY_CREATED_AT + " TEXT" + ")";
        db.execSQL(CREATE_LOGIN_TABLE);
        String CREATE_TABLE_TRACK = "CREATE TABLE " + TABLE_TRACK + "("
                + KEY_UIDTRACK + " INTEGER PRIMARY KEY," + KEY_DOCKETNO
                + "INTEGER PRIMARY KEY," + KEY_DEALERNAME + " TEXT,"
                + KEY_CUSTOMERNAME + " TEXT ," + KEY_PCS
                + "INTEGER PRIMARY KEY," + KEY_DELIVERY_STATUS + " TEXT ,"
                + KEY_REMARKS + " TEXT ," + KEY_CREATED_AT_TRACK + " TEXT"
                + ")";
        db.execSQL(CREATE_TABLE_TRACK);
    }
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRACK);
        // Create tables again
        onCreate(db);
    }
    /**
     * Storing user details in database
     * */
    public void addUser(String name, String email, String uid, String created_at) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name); // Name
        values.put(KEY_EMAIL, email); // Email
        values.put(KEY_UID, uid); // Email
        // Inserting Row
        db.insert(TABLE_LOGIN, null, values);
        db.close(); // Closing database connection
    }
    /**
     * Storing AeyeTrack details in database
     * */
    public void addAeyeTrack(Integer idtrack, Integer docketno,
            String dealername, String customername, Integer pcs,
            String deliverstatus, String remarks, String created_at_track) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_UIDTRACK, idtrack); // idtrack
        values.put(KEY_DOCKETNO, docketno); // docketno
        values.put(KEY_DEALERNAME, dealername); // dealername
        values.put(KEY_CUSTOMERNAME, customername); // customername
        values.put(KEY_PCS, pcs); // pcs
        values.put(KEY_DELIVERY_STATUS, deliverstatus); // deliverstatus
        values.put(KEY_REMARKS, remarks); // remarks
        values.put(KEY_CREATED_AT_TRACK, created_at_track); // created_at_track
        // Inserting Row
        db.insert(TABLE_TRACK, null, values);
        db.close(); // Closing database connection
    }
    /**
     * Getting update AeyeTrack data from database
     * */
    public boolean updateAeyeTrack(Integer idtrack, Integer docketno,
            String dealername, String customername, Integer pcs,
            String deliverstatus, String remarks, String created_at_track) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_UIDTRACK, idtrack); // idtrack
        contentValues.put(KEY_DOCKETNO, docketno); // docketno
        contentValues.put(KEY_DEALERNAME, dealername); // dealername
        contentValues.put(KEY_CUSTOMERNAME, customername); // customername
        contentValues.put(KEY_PCS, pcs); // pcs
        contentValues.put(KEY_DELIVERY_STATUS, deliverstatus); // deliverstatus
        contentValues.put(KEY_REMARKS, remarks); // remarks
        contentValues.put(KEY_CREATED_AT_TRACK, created_at_track); // created_at_track
        db.update("track", contentValues, "idtrack = ? ",
                new String[] { Integer.toString(idtrack) });
        return true;
    }
    /**
     * Getting AeyeTrack data from database
     * */
    public HashMap<String, String> getAeyeTrackDetails() {
        HashMap<String, String> AeyeTrack = new HashMap<String, String>();
        String selectQuery = "SELECT  * FROM " + TABLE_TRACK;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            AeyeTrack.put("idtrack", cursor.getString(1));
            AeyeTrack.put("docketno", cursor.getString(2));
            AeyeTrack.put("uid", cursor.getString(3));
            AeyeTrack.put("dealername", cursor.getString(4));
            AeyeTrack.put("customername", cursor.getString(5));
            AeyeTrack.put("pcs", cursor.getString(6));
            AeyeTrack.put("deliverstatus", cursor.getString(7));
            AeyeTrack.put("remarks", cursor.getString(8));
            AeyeTrack.put("created_at_track", cursor.getString(9));
        }
        cursor.close();
        db.close();
        // return user
        return AeyeTrack;
    }
    /**
     * Getting user data from database
     * */
    public HashMap<String, String> getUserDetails() {
        HashMap<String, String> user = new HashMap<String, String>();
        String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            user.put("name", cursor.getString(1));
            user.put("email", cursor.getString(2));
            user.put("uid", cursor.getString(3));
            user.put("created_at", cursor.getString(4));
        }
        cursor.close();
        db.close();
        // return user
        return user;
    }
    /**
     * Getting user login status return true if rows are there in table
     * */
    public int getRowCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();
        // return row count
        return rowCount;
    }
    /**
     * Re create database Delete all tables and create them again
     * */
    public void resetTables() {
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete(TABLE_LOGIN, null, null);
        db.delete(TABLE_TRACK, null, null);
        db.close();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >
        <!-- DocketNo. TextField -->
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Docket_no" />
        <!-- DocketNo. EditField -->
        <EditText
            android:id="@+id/Docket_number"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10" />
        <!-- Submit Button -->
        <Button
            android:id="@+id/btnSubmit"
            android:layout_width="158dp"
            android:layout_height="wrap_content"
            android:text="Submit" />
        <!-- Dealer_name TextField -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Dealer_name" />
        <!-- Dealer_name. EditField -->
        <EditText
            android:id="@+id/Dealr_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <!-- Customer_name TextField -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Customer_name" />
        <!-- Customer_name EditField -->
        <EditText
            android:id="@+id/Custmer_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <!-- Pcs TextField -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Pcs" />
        <!-- Pcs EditField -->
        <EditText
            android:id="@+id/Pcs_edit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <!-- Delivery_Status TextField -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Delivery_Status" />
        <!-- Delivery_Status. EditField -->
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/Delivery_Status_arrays"
            android:prompt="@string/Delivery_Status_prompt" />
        <!-- Remarks TextField -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Remarks" />
        <!-- Remarks_Edit EditField -->
        <EditText
            android:id="@+id/Remarks_Edit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <!-- "Next" Button -->
        <!-- Cancel Button -->
        <Button
            android:id="@+id/Next"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="Next" />
        <Button
            android:id="@+id/btnCancel"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="Cancel" />
    </LinearLayout>
</ScrollView>

试试这个:

Docket_number.setText(yourHashMap.get("key"));

获取它的最简单方法是读取 HashMap() 的键;

DatabaseHandler d = new DatabaseHandler(this);
        HashMap<String, String> getdetails = d.getUserDetails();
        String name = getdetails.get("name");
        String email = getdetails.get("email");
        String uid = getdetails.get("uid");
        String created_at = getdetails.get("created_at");

        System.out.println("Name-->" + name);
        System.out.println("Email-->" + email);
        System.out.println("UID-->" + uid);
        System.out.println("Created At-->" + created_at);

获取所有值后,您可以将它们设置为您想要的 EditText

注意:将此代码放在 Activity 的 onCreate 方法中,您在其中拥有 EditText,但在初始化 EditTexts 之后 您可以使用相同的方法获取摘要详细信息