lundi 9 mars 2015

how to display one row from SQLite database to textview/edittext

I'm very new to android and about to create an app which holds customers and then related to it some actions. My current unsolvable problem: After inserting customer data into database I want to switch the view and display the last entry with all fields. The inserted values like title, name but also automatically created fields like id and date. I can see that the data are in the database when I pull the database file from the DDMS but I don't know how to retrieve and display them in the desired view. I saw dozens of examples for passing it to a listview but I couldn't find any example which would suit my needs. Maybe it's because I don't know how to ask correctly for it. If so, please point me in the right direction.


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


}


And my 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();



}

}

}

}


I'm looking for a solution to implement this view with the last entry into my CustomerHelperClass so that I can also modify or delete this entry.


For Inserting new customer I have add_new_customer.xml and I want to switch view to update_customer.xml with the last entry displayed. Both of them have the text fields for title, name etc


Aucun commentaire:

Enregistrer un commentaire