 |
|
คือตอนนี้ผมสามารถใช้AutoCompleteTextView เรียกข้อมูลจาก sqlite ได้แล้วครับ แต่ปัญหาคือ
จะต้องทำอย่างไร ให้เมื่อกดปุ่ม search แล้วมันจะ get ค่าในช่อง AutoCompleteTextView ไปยังอีก activityอื่น และแสดงข้อมูลทั้งหมดครับ
เพราะผมทำ activity.xml ไว้สำหรับแสดงค่าทั้งหมดที่ search
รบกวนด้วยครับ
ผมพยายามใช้คำสั่งนี้ใน AutoComActivity แต่มันใช้ไม่ได้ครับ
AutoComActivity
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);
}
}
activity_detail
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Member Detail : "
android:layout_span="3"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:text="MemberID : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtMemberID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MemberID" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView3"
android:text="Password : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password " />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView4"
android:text="Name : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView5"
android:text="Tel : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtTel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tel" />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView6"
android:text="Email : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView7"
android:text="Address : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address" />
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_marginLeft="140dp"
/>
<Button
android:id="@+id/btnEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:text="Edit"
/>
</LinearLayout>
</TableLayout>
Tag : Mobile, Android, Windows, Mobile
|
ประวัติการแก้ไข 2016-05-16 11:46:21 2016-05-16 13:35:11
|
 |
 |
 |
 |
Date :
2016-05-16 11:35:43 |
By :
rattapongza |
View :
1317 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |