|
|
|
Android - ส่งตัวแปรเข้าไปในไฟล์ SQLiteOpenHelper ยังไงครับ ใช้ intent ไม่ได้ |
|
|
|
|
|
|
|
เขียนไว้แบบไหน ลองเอา Code มาดูหน่อยครับ
|
|
|
|
|
Date :
2014-02-07 13:45:22 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Android-Java)
public class SearchActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
public void search(View v){
EditText search = (EditText) findViewById(R.id.txtSearch);
String txtS = search.getText().toString();
Intent intent = new Intent(this, MyDBClass.class);
intent.putExtra("AA", txtS);
startActivity(intent);
final MyDBClass myDb = new MyDBClass(this);
final ArrayList<HashMap<String, String>> MebmerList = myDb.SelectAllData();
// listView1
ListView lisView1 = (ListView)findViewById(R.id.listView1);
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(SearchActivity.this, MebmerList, R.layout.activity_column,
new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});
if(txtS.equals(sAdap)){
lisView1.setAdapter(sAdap);
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
}
});
}
}
Code (Android-Java)
public class MyDBClass extends SQLiteOpenHelper {
Intent intent = getIntent();
String txtS = intent.getStringExtra("AA");
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mydatabase";
// Table Name
private static final String TABLE_MEMBER = "members";
public MyDBClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_MEMBER +
"(MemberID INTEGER PRIMARY KEY AUTOINCREMENT," +
" Name TEXT(100)," +
" Tel TEXT(100));");
Log.d("CREATE TABLE","Create Table Successfully.");
}
// Search All Data
public ArrayList<HashMap<String, String>> SearchAllData() {
// TODO Auto-generated method stub
try {
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT * FROM "+ TABLE_MEMBER +" WHERE Name like "+ txtS +" ORDER BY Tel" ;
//Select MemberID from member where name like '%txtSearch%'
//"SELECT * FROM " + TABLE_MEMBER
Cursor cursor = db.rawQuery(strSQL, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
do {
map = new HashMap<String, String>();
map.put("MemberID", cursor.getString(0));
map.put("Name", cursor.getString(1));
map.put("Tel", cursor.getString(2));
MyArrList.add(map);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return MyArrList;
} 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 " + TABLE_MEMBER);
// Re Create on method onCreate
onCreate(db);
}
}
|
|
|
|
|
Date :
2014-02-08 08:57:53 |
By :
spwv260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ส่งค่า parameter ไปที่ method seachalldataก็ไม่ได้ครับ
|
|
|
|
|
Date :
2014-02-10 07:52:11 |
By :
spwv260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|