从数据库获取的值不会编辑

时间:2013-05-02 13:38:42

标签: android sqlite

我正在开发一个数据库应用程序。我成功插入数据,但数据不会编辑。以下是我的编辑课请帮助解决我的问题....

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;
}
}

有没有国旗问题?在编辑。我还在这里使用了flag来添加值类和编辑值类。在哪里设置true以及在哪里设置false?这是问题吗? 我尝试了很多,但是值不是编辑.....请建议解决方案。

1 个答案:

答案 0 :(得分:0)

您的更新代码似乎有误。 db.update的参数应该是: update(String表,ContentValues值,String whereClause,String [] whereArgs)

您似乎试图在whereClause中传递整个内容,而不是将您使用的值放在whereArgs中。

我认为应该是: db.update(T_DLICENSE,values,DLID +“=?”,new String [] {String.valueOf(rowid)});