|
|
|
Android - ต้องการดึงข้อมูลจาก SQLite แสดงที่ edittext ต้องทำอยางงัยครับ ผมลองทำแล้วมัน error ครับ ช่วยหน่อยนะครับ |
|
|
|
|
|
|
|
Code (Java)
package pro.example.project;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class UseSV extends Activity {
private static final String TABLE_data = null;
private EditText num1;
private EditText num2,num3;
private TextView tv;
private SQLiteDatabase db;
private EditText etId, etNum, etTax,etDat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.usesv);
etId= (EditText) findViewById(R.id.api_id);
num1 = (EditText) findViewById(R.id.api_num);
num2 = (EditText) findViewById(R.id.api_tax);
num3 = (EditText) findViewById(R.id.api_dat);
// num1.setInputType(0);
num2.setInputType(0);
tv = (TextView) findViewById(R.id.api_tv);
registerReceiver(br, new IntentFilter("com.mycomp.intent.r1")); //เป็นการสร้าง filter ด้วภาษาจาวา แทน xml
try{
db = openOrCreateDatabase("mydb.db", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE data(_id INTEGER PRIMARY KEY , num REAL, tax REAL, dat REAL);");
}catch (Exception e){
Log.i("mylog","Error Database "+e);
}
}
public void doInsert(View v) {
String num = num1.getText().toString();
String tax = num2.getText().toString();
String dat = num3.getText().toString();
db.execSQL("INSERT INTO data (num , tax, dat) VALUES('" + num + "', '" + tax + "', '" + dat + "')");
tv.setText("Insert ok.");
}
public void doUpdate(View v) {
String id = etId.getText().toString();
String num = num1.getText().toString();
String tax = num2.getText().toString();
String dat = num3.getText().toString();
db.execSQL("UPDATE data SET num = '"+num+"', tax = '"+tax+"', dat = '"+dat+"'WHERE _id = '"+id+"'");
tv.setText("Update ok.");
}
public void doDelete(View v) {
String id = etId.getText().toString();
db.execSQL("DELETE FROM data WHERE _id = '" + id + "'");
tv.setText("Delete ok.");
}
// Select Data
public String[] SelectData(String strMemberID) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_data, new String[] { "*" },
"id=?",
new String[] { String.valueOf(strMemberID) }, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
/***
* 0 = MemberID
* 1 = Name
* 2 = Tel
*/
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
arrData[2] = cursor.getString(2);
arrData[3] = cursor.getString(3);
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
public void doQuery(View v) {
String id = etId.getText().toString();
//String num1 = num1.getText().toString();
Cursor cs;
if (id.equals("")) {
Toast.makeText(UseSV.this, "กรุณากรอก ID ด้วยค่ะ",Toast.LENGTH_LONG).show();
return;
} else {
cs = db.rawQuery("SELECT * FROM data WHERE _id = "+id, null);
String[] arrData = new String[cs.getColumnCount()];
arrData[0] = cs.getString(0);
arrData[1] = cs.getString(1);
arrData[2] = cs.getString(2);
arrData[3] = cs.getString(3);
num1.setText(arrData[1].toString());
num2.setText(arrData[2].toString().toString());
num3.setText(arrData[3].toString().toString());
}
//String id = cs.getString("id");
//String num0 = cs.getDouble("num")
//String from[] = {"num","tax","dat"};
//int to[] = { R.id.api_num};
//SimpleCursorAdapter sc = new SimpleCursorAdapter(this,R.layout.usesv, cs, from, to);
//tv.setText("");
//num1.setText([0].toString().toString());
//num2.setText(from[1].toString().toString());
//num3.setText(from[2].toString().toString());
//num1.setText(to.toString());
}
private SQLiteDatabase getReadableDatabase() {
// TODO Auto-generated method stub
return null;
}
public void doAdd(View v) {
Intent i = new Intent(this, AddService.class);
i.putExtra("num1", num1.getText().toString());
i.putExtra("num2", num2.getText().toString());
i.putExtra("num3", num3.getText().toString());
startService(i);
}
public void doHide(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), // the window token to be dismissed
0); // dismiss flag
}
public void doShow(View v){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, // show flag
InputMethodManager.HIDE_IMPLICIT_ONLY); // hide flag
}
private BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
tv.setText("ผลลัพธ์ = "+intent.getStringExtra("result"));
}
};
}
Tag : Mobile
|
|
|
|
|
|
Date :
2014-02-21 19:13:47 |
By :
jadiloke |
View :
1364 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error อะไรครับ ? มี LogCat ไหม๊
|
|
|
|
|
Date :
2014-02-22 10:08:44 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|