2013-05-02 31 views
0

我正在開發一個數據庫應用程序。我成功地插入了數據,但數據不會被編輯。以下是我的編輯類,請幫忙解決我的問題....從數據庫中提取的值不會編輯

import java.util.Calendar; 

import com.smartwallet.database.DbAdapterDLicense; 

import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Edit_DLicense extends Activity implements OnClickListener 
{ 
private EditText et_cnm,et_idt,et_edt,et_lno,et_add,et_cno,et_ltype; 
private Button bt_add; 

private Long dlid; 
private Cursor c; 
private int mYear,mMonth, mDay; 
static final int DATE_DIALOG_IDI = 0; 
static final int DATE_DIALOG_IDE = 1; 
private String imei_id; 
private DatePickerDialog.OnDateSetListener mDateSetListenerI; 
private DatePickerDialog.OnDateSetListener mDateSetListenerE; 
private DbAdapterDLicense DBA; 


@Override 
public void onCreate(Bundle b) 
{ 
    super.onCreate(b); 
    setContentView(R.layout.editapp); 

    dlid=null; 
    Bundle extras=getIntent().getExtras(); 

    dlid=(b==null)? null:(Long)b.getSerializable("imei_id"); 
    if(extras!=null) 
    { 
     dlid=extras.getLong("imei_id"); 
    } 



    DBA=new DbAdapterDLicense(this); 
    DBA.open(); 

    et_cnm=(EditText)findViewById(R.id.et_cnm); 
    et_idt=(EditText)findViewById(R.id.et_idt); 
    et_edt=(EditText)findViewById(R.id.et_edt); 
    et_lno=(EditText)findViewById(R.id.et_lno); 
    et_add=(EditText)findViewById(R.id.et_add); 
    et_cno=(EditText)findViewById(R.id.et_cno); 
    et_ltype=(EditText)findViewById(R.id.et_ltype); 
    et_idt.setOnClickListener(this); 
    et_edt.setOnClickListener(this); 

    bt_add=(Button)findViewById(R.id.bt_save); 
    bt_add.setOnClickListener(this);   

    mDateSetListenerI = new DatePickerDialog.OnDateSetListener() 
    { 
      public void onDateSet(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) 
      { 
       mYear = year; 
       mMonth = monthOfYear; 
       mDay = dayOfMonth; 
       updateDisplay1(); 
      } 
     }; 

     mDateSetListenerE = new DatePickerDialog.OnDateSetListener() 
     { 
       public void onDateSet(DatePicker view, int year, 
             int monthOfYear, int dayOfMonth) 
       { 
        mYear = year; 
        mMonth = monthOfYear; 
        mDay = dayOfMonth; 
        //updateDisplay(); 
       } 
      }; 

    final Calendar c = Calendar.getInstance(); 
    mYear = c.get(Calendar.YEAR); 
    mMonth = c.get(Calendar.MONTH); 
    mDay = c.get(Calendar.DAY_OF_MONTH); 

    //et_cdt.setText(mDay+"/"+(mMonth+1)+"/"+mYear); 

    updateDisplay1(); 
    updateDisplay2(); 
    updateDL(); 
} 

public void updateDL() 
{ 
    if(dlid!= null) 
    { 
     c = DBA.fetchDL_imei(dlid); 
     startManagingCursor(c); 
     if(c.moveToFirst()) 
     { 
      imei_id=c.getString(1); 
      et_cnm.setText(c.getString(2)); 
      et_idt.setText(c.getString(3)); 
      et_edt.setText(c.getString(4)); 
      et_lno.setText(c.getString(5)); 
      et_add.setText(c.getString(6)); 
      et_cno.setText(c.getString(7)); 
      et_ltype.setText(c.getString(8)); 

      bt_add.setText("Save"); 
     } 
    } 
} 
@Override 
public void onClick(View v) 
{ 
    // TODO Auto-generated method stub 
    if(v.equals(bt_add)) 
    { 
     if(dlid==null) 
     { 
      DBA.updateDL(dlid,Long.parseLong(imei_id),et_cnm.getText().toString(),et_idt.getText().toString(),et_edt.getText().toString(),et_lno.getText().toString(),et_add.getText().toString(),et_cno.getText().toString(),et_ltype.getText().toString(),"true"); 
      Toast.makeText(Edit_DLicense.this, "Item Updated"+c.getCount(), 2000).show(); 
      //finish(); 
     } 
     finish(); 
    } 
    else if(v.equals(et_idt)) 
    { 
     showDialog(DATE_DIALOG_IDI); 
    } 
    else if(v.equals(et_edt)) 
    { 
     showDialog(DATE_DIALOG_IDE); 
    } 
} 

private void updateDisplay1() 
{ 
    et_idt.setText(""+mDay+"/"+(mMonth+1)+"/"+mYear); 
} 
private void updateDisplay2() 
{ 
    et_edt.setText(""+mDay+"/"+(mMonth+1)+"/"+mYear); 
} 

@Override 
protected Dialog onCreateDialog(int id) 
{ 
    if(id==DATE_DIALOG_IDI) 
    { 
     return new DatePickerDialog(this, 
       mDateSetListenerI, 
       mYear, mMonth, mDay); 
    } 
    else if(id==DATE_DIALOG_IDE) 
    { 
     return new DatePickerDialog(this, 
       mDateSetListenerE, 
       mYear, mMonth, mDay); 
    } 
    return null; 
} 
@Override 
protected void onDestroy() 
{ 
    // TODO Auto-generated method stub 
    DBA.close(); 
    super.onDestroy(); 
} 
} 

,這是我的適配器類

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 

public class DbAdapterDLicense 
{ 

public static final String DLID = "dlid"; 
public static final String IMEI_ID="imei_id"; 
public static final String U_NAME="c_nm"; 
public static final String I_DATE="i_date"; 
public static final String E_DATE = "e_date"; 
public static final String L_NO = "l_no"; 
public static final String CL_NO="cl_no"; 
public static final String C_ADDR="c_addr"; 
public static final String L_TYPE="l_type"; 
public static final String EXE_FLAG="exe_flg"; 
private static final String T_DLICENSE = "DrivingLicense"; 

private Context context; 
private SQLiteDatabase db; 
private DbHelper dbHelper; 

public DbAdapterDLicense(Context context) 
{ 
    this.context = context; 
} 

public DbAdapterDLicense open() throws SQLException 
{ 
    dbHelper = new DbHelper(context); 
    db = dbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() 
{ 
    dbHelper.close(); 
} 

//For Appointment Table 
public long insertDL(long imei_id,String c_nm,String i_date, String e_date,String l_no,String cl_no,String c_addr,String l_type,String exe_flg) 
{ 
    ContentValues values = createContentValues_dl(imei_id,c_nm,i_date, e_date, l_no,cl_no,c_addr,l_type,exe_flg); 
    return db.insert(T_DLICENSE, null, values); 
} 

public boolean updateDL(long rowId, long imei_id,String c_nm,String i_date, String e_date,String l_no,String cl_no,String c_addr,String l_type,String exe_flg) 
{ 
    ContentValues values = createContentValues_dl(imei_id,c_nm,i_date, e_date, l_no,cl_no,c_addr,l_type,exe_flg); 
    return db.update(T_DLICENSE, values, DLID + "=" + rowId, null) > 0; 
} 

public boolean updateDL_imei(long rowId, long imei_id,String c_nm,String i_date, String e_date,String l_no,String cl_no,String c_addr,String l_type,String exe_flg) 
{ 
    ContentValues values = createContentValues_dl(imei_id,c_nm,i_date, e_date, l_no,cl_no,c_addr,l_type,exe_flg); 
    return db.update(T_DLICENSE, values, IMEI_ID + "=" + rowId, null) > 0; 
} 

public boolean deleteDL(long rowId) 
{ 
    return db.delete(T_DLICENSE, IMEI_ID + "=" + rowId, null) > 0; 
} 

public Cursor fetchAllDL() 
{ 
    return db.query(T_DLICENSE, new String[] { DLID, IMEI_ID,U_NAME,I_DATE,E_DATE,L_NO,CL_NO,C_ADDR,L_TYPE,EXE_FLAG}, null, null, null, null, null); 
} 

public Cursor fetchDL(long rowId) throws SQLException 
{ 
    Cursor mCursor = db.query(true,T_DLICENSE, new String[] {DLID, IMEI_ID,U_NAME,I_DATE,E_DATE,L_NO,CL_NO,C_ADDR,L_TYPE,EXE_FLAG}, DLID + "=" 
      + rowId, null, null, null, null, null); 
    if (mCursor != null) 
    { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

public Cursor fetchDL_imei(long rowId) throws SQLException 
{ 
    Cursor mCursor = db.query(true,T_DLICENSE, new String[] {DLID, IMEI_ID,U_NAME,I_DATE,E_DATE,L_NO,CL_NO,C_ADDR,L_TYPE,EXE_FLAG}, IMEI_ID + "=" 
      + rowId, null, null, null, null, null); 
    if (mCursor != null) 
    { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

private ContentValues createContentValues_dl(long imei_id,String c_nm,String i_date, String e_date,String l_no,String cl_no,String c_addr,String l_type,String exe_flg) 
{ 
    ContentValues values = new ContentValues(); 
    values.put(IMEI_ID, imei_id); 
    values.put(U_NAME, c_nm); 
    values.put(I_DATE, i_date); 
    values.put(E_DATE, e_date); 
    values.put(L_NO, l_no); 
    values.put(CL_NO, cl_no); 
    values.put(C_ADDR, c_addr); 
    values.put(L_TYPE, l_type); 
    values.put(EXE_FLAG, exe_flg); 
    return values; 
} 
} 

是有標誌的任何問題?在編輯中。我也在這裏使用標誌來添加值類和編輯值類。在哪裏設置真假以及在哪裏設置假?這是問題嗎? 我嘗試了很多,但th值不是編輯.....請建議解決方案。

回答

0

您的代碼更新似乎是錯誤的。 db.update的參數應該是: update(String table,ContentValues values,String whereClause,String [] whereArgs)

您似乎試圖在whereClause中傳遞整個事物,而不是將您的值正在使用whereArgs。我相信應該是: db.update(T_DLICENSE,values,DLID +「=?」,new String [] {String.valueOf(rowid)});

+0

哦!謝謝,但它不能解決我的問題.....更多的解決方案..請 – user2050129 2013-05-02 16:38:05

+0

我認爲你已經通過你的代碼檢查,當你試圖運行更新dlid不爲空?你有錯誤嗎?您也可以在事務中包裝更新,例如db.beginTransaction();嘗試{//運行編輯然後db.setTransactionSuccessful();} catch(Exception){//顯示錯誤} finally {db.endTransaction();} – Matt 2013-05-02 20:59:54

+0

nope它沒有給出任何錯誤或異常,但也沒有更新編輯的值I刪除了dlid == null語句,但仍然沒有更新值。 – user2050129 2013-05-03 01:38:35