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
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>
edit:
} 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)));
}
}
}
Hopefully you can build upon this answer, if you need further guidance, feel free to comment.
Let's assume you want the EditText
with id ntxt_first_name
to be populated with customer name. You'd write this code after you call 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)));
}
The above snippet tries to find the View
( EditText
) with id ntxt_frist_name
within the inflated View hierarchy that was supplied using the setContentView(int resourceId)
method (hopefully you have supplied R.layout.update_customer
as its parameter. Then it simply sets its mText
field to the value it obtains from the entry cursor
is currently pointing at (set to last entry in the query by your displayLastCustomer()
method) for the column DbHelper.COLUMN_CUSTOMER_FIRST_NAME
.
Like I already wrote, I hope you can get from this snippet how to populate other Views obtaining data from other columns in your Cursor
object.
Cheers and good luck!
EDIT: In case you are not running this code inside an Activity
object (or object of a class derived from it) , you need to obtain the Context
of the Activity
which has the update_customer.xml
file inflated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.