Android - ช่วยดูหน้า Show ให้หน่อยมันไม่ยอมไปหน้าอัพเดทข้อมูล
Showdata
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import com.projectDB.project.DBTime;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.widget.Toast;
public class Showdata extends Activity {
ArrayList<HashMap<String, String>> myData1;
// ตัวแปละtime
private EditText editT2;
private Button btn2;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showdata);
// เวลา ************
editT2 = (EditText) findViewById(R.id.edtday2);
btn2 = (Button) findViewById(R.id.btnday2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateCurrentDate();
// get Data from SQLite
final DBTime myDb = new DBTime(this);
final Cursor myData = myDb.SelectAllData();
// listView1
ListView lisView1 = (ListView)findViewById(R.id.listView1);
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(Showdata.this, R.layout.column, myData
,new String[] {"day","Duration1","Duration1Time","Duration2","Duration2Time","Duration3","Duration3Time"}
,new int[] {R.id.Colname, R.id.Colc1, R.id.Colc1Time, R.id.Colc2, R.id.Colc2Time, R.id.Colc3, R.id.Colc3Time});
lisView1.setAdapter(adapter);
// OnClick Item
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
// Show on new activity
Intent newActivity = new Intent(Showdata.this,UpdateData.class);
newActivity.putExtra("day", myData1.get(position).get("day").toString());
startActivity(newActivity);
}
});
}
ฐานข้อมูล
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;
public class DBTime extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase";
private static final String TABLE_Time = "Time";
private static final String TABLE_data = "data";
public DBTime(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/* private static final String DBdata = "" +
"CREATE TABLE data (" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT ," +
" day TEXT(20)," +
" Duration1 TEXT(20)," +
" Duration2 TEXT(20)," +
" Duration3 TEXT(20)," +
" num TEXT(10));";
private static final String DBTime = "" +
"CREATE TABLE Time (" +
"ID INTEGER PRIMARY KEY," +
" Morning1 TEXT(10)," +
" Morning2 TEXT(10)," +
" Morning3 TEXT(10)," +
" Morning4 TEXT(10));";
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_Time +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
" Morning1 TEXT(100)," +
" Morning2 TEXT(100)," +
" Morning3 TEXT(100)," +
" Morning4 TEXT(100));");
db.execSQL("CREATE TABLE " + TABLE_data +
"( ID INTEGER PRIMARY KEY AUTOINCREMENT ," +
" day TEXT(100)," +
" Duration1 TEXT(100)," +
" Duration1Time TEXT(100)," +
" Duration2 TEXT(100)," +
" Duration2Time TEXT(100)," +
" Duration3 TEXT(100)," +
" Duration3Time TEXT(100));");
Log.d("CREATE TABLE","Create Table Successfully.");
}
// Select All Data
public Cursor SelectAllData() {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT ID As _id , * FROM " +TABLE_data ;
Cursor cursor = db.rawQuery(strSQL, null);
return cursor;
} catch (Exception e) {
return null;
}
}
// Update Data
public long UpdateData(String sID,String sday,String sDuration1,String sDuration1Time,String sDuration2,String sDuration2Time,String sDuration3,String sDuration3Time) {
// 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("day", sday);
Val.put("Duration1", sDuration1);
Val.put("sDuration1Time", sDuration1Time);
Val.put("Duration2", sDuration2);
Val.put("sDuration2Time", sDuration2Time);
Val.put("Duration3", sDuration3);
Val.put("sDuration3Time", sDuration3Time);
long rows = db.update(TABLE_data, Val, " ID = ?",
new String[] { String.valueOf(sID) });
db.close();
return rows; // return rows updated.
} catch (Exception e) {
return -1;
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//พิมพ์ Log เพื่อให้เห็นว่ามีการ Upgrade Database
Log.w(DBTime.class.getName(),
"Upgread database version from version" + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
//ลบตาราง member ของเก่าทิ้ง
db.execSQL("DROP TABLE IF EXISTS Time");
db.execSQL("DROP TABLE IF EXISTS data");
onCreate(db);
}
}
Tag : Mobile, Android
Date :
2013-11-09 06:37:17
By :
bbza
View :
1075
Reply :
6
คงจะต้องใช้การ Debug ดูแล้วครับ
Date :
2013-11-09 09:08:15
By :
mr.win
พอกด List showในมันก็แดงแบบนี้
Date :
2013-11-09 15:15:25
By :
bbza
ผมรับรัยผิด
Code (Android-Java)
// Show on new activity
Intent newActivity = new Intent(Showdata.this,UpdateData.class);
newActivity.putExtra("day", myData1.get(position).get("day").toString());
startActivity(newActivity);
Date :
2013-11-09 23:20:08
By :
bbza
myData1 ยังไม่ได้ assign variable ให้มันนิครับ
Date :
2013-11-10 13:54:22
By :
devsharing
เห็นมีคำว่า Null น่าจะมีค่าอะไรว่างอยู่ครับ
Date :
2013-11-10 15:39:04
By :
mr.win
Load balance : Server 03