HOME > Mobile > Mobile Forum > คือตอนนี้ผมสามารถใช้AutoCompleteTextView เรียกข้อมูลจาก sqlite ได้แล้ว อยากจะนำค่าที่เรียกได้ไปแสดงรายละเอียดในอีก activity ต้องทำยังไงหรอครับ
final myDBClass myDb = new myDBClass(this);
final ArrayList<HashMap<String, String>> MebmerList = myDb.SelectAllData();
Button button1 = (Button)findViewById(R.id.button1);
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(AutoComActivity.this, MebmerList, R.layout.activity_column,
new String[] {"Name"}, new int[] {R.id.ColName});
button1.setAdapter(sAdap);
button1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
// Show on new activity
Intent newActivity = new Intent(AutoComActivity.this,DetailActivity.class);
newActivity.putExtra("MemID", MebmerList.get(position).get("Name").toString());
startActivity(newActivity);
}
});
myDBClass
package com.example.rattapongt.androidproject;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
public class myDBClass extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase";
private static final String TABLE_MEMBER = "members";
public myDBClass(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_MEMBER + "(MemberID INTEGER PRIMARY KEY AUTOINCREMENT," + " Pass TEXT(100)," + " Name TEXT(100)," + " Tel TEXT(100)," + " Email TEXT(100)," + " Address TEXT(100));");
Log.d("CREATE TABLE","Create Table Successfully");
}
public long InsertData(String strMemberID, String strPassword, String strName, String strTel, String strEmail, String strAddress){
try {
SQLiteDatabase db;
db = this.getWritableDatabase();
ContentValues Val = new ContentValues();
Val.put("MemberID", strMemberID);
Val.put("Pass", strPassword);
Val.put("Name", strName);
Val.put("Tel", strTel);
Val.put("Email", strEmail);
Val.put("Address", strAddress);
long rows = db.insert(TABLE_MEMBER, null, Val);
return rows;
}catch (Exception e){
return -1;
}
}
public String[] SelectData(String strMemberID){
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" },
"MemberID=?",
new String[] { String.valueOf(strMemberID) }, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst()){
arrData = new String[cursor.getColumnCount()];
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
arrData[2] = cursor.getString(2);
arrData[3] = cursor.getString(3);
arrData[4] = cursor.getString(4);
arrData[5] = cursor.getString(5);
}
}
cursor.close();
db.close();
return arrData;
}catch (Exception e){
return null;
}
}
public ArrayList<HashMap<String, String>> SelectAllData() {
// TODO Auto-generated method stub
try {
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<>();
HashMap<String, String> map;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT * FROM " + TABLE_MEMBER;
Cursor cursor = db.rawQuery(strSQL, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
do {
map = new HashMap<>();
map.put("MemberID", cursor.getString(0));
map.put("Pass", cursor.getString(1));
map.put("Name", cursor.getString(2));
map.put("Tel", cursor.getString(3));
map.put("Email", cursor.getString(4));
map.put("Address", cursor.getString(5));
MyArrList.add(map);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return MyArrList;
} catch (Exception e) {
return null;
}
}
public long UpdateData(String strMemberID, String strPass,String strName ,String strTel, String strEmail, String strAddress) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
/**
* for API 11 and above
SQLiteStatement insertCmd;
String strSQL = "UPDATE " + TABLE_MEMBER
+ " SET Name = ? "
+ " , Tel = ? "
+ " WHERE MemberID = ? ";
insertCmd = db.compileStatement(strSQL);
insertCmd.bindString(1, strName);
insertCmd.bindString(2, strTel);
insertCmd.bindString(3, strMemberID);
return insertCmd.executeUpdateDelete();
*
*/
ContentValues Val = new ContentValues();
Val.put("Pass", strPass);
Val.put("Name", strName);
Val.put("Tel", strTel);
Val.put("Email", strEmail);
Val.put("Address", strAddress);
long rows = db.update(TABLE_MEMBER, Val, " MemberID = ?",
new String[] { String.valueOf(strMemberID) });
db.close();
return rows; // return rows updated.
} catch (Exception e) {
return -1;
}
}
// Delete Data
public long DeleteData(String strMemberID) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
long rows = db.delete(TABLE_MEMBER, "MemberID = ?",
new String[] { String.valueOf(strMemberID) });
db.close();
return rows; // return rows deleted.
} catch (Exception e) {
return -1;
}
}
// Select Data
public String getSingleEntry(String strMemberID) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor=db.query("members", null, " MemberID=?", new String[]{strMemberID}, null, null, null, null);
if (cursor.getCount()<1)
{
cursor.close();
return "NOT EXIST";
}
if (cursor.moveToFirst()) {
String Tel= cursor.getString(cursor.getColumnIndex("Pass"));
cursor.close();
return Tel;
}
cursor.close();
db.close();
} catch (Exception e) {
return null;
}
return strMemberID;
}
public void updateEntry(SQLiteDatabase db,String strMemberID)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("MemberID", strMemberID);
String where="MemberID = ?";
db.update("members",updatedValues, where, new String[]{strMemberID});
}
// Select All Data
public String[] SelectAllData1() {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<>();
HashMap<String, String> map;
String strSQL = "SELECT NAME FROM " + TABLE_MEMBER;
Cursor cursor = db.rawQuery(strSQL, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getCount()];
int i= 0;
do {
arrData[i] = cursor.getString(0);
i++;
} while (cursor.moveToNext());
}
}
cursor.close();
return arrData;
} catch (Exception e) {
return null;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + "members");
// Re Create on method onCreate
onCreate(db);
}
}
AutoComActivity)
package com.example.rattapongt.androidproject;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.app.Activity;
import android.widget.Toast;
public class AutoComActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_com);
final myDBClass myDb = new myDBClass(this);
final String [] myData = myDb.SelectAllData1();
// autoCompleteTextView1
final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, myData);
autoCom.setAdapter(adapter);
// button1
final Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(AutoComActivity.this,
String.valueOf("Your Input : " + autoCom.getText().toString()),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public MenuInflater getMenuInflater() {
return new MenuInflater(this);
}
}