如何更新单击的行

时间:2014-05-05 13:52:13

标签: android sql database

使用下面的代码我能够更新id与updateDiaryEntry中给出的数字相对应的行,例如。第2行:

private void saveItToDB(long rowId) {
    dba.open();
    dba.updateDiaryEntry(((TextView) editText).getText().toString(), 2);
    dba.close();
    ((TextView) editText).setText("");
}

虽然,当我尝试使用rowId时,没有任何内容得到更新。我尝试更新单击的行,因此这里有更多代码:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //Position is the number of the item clicked
    //You can use your adapter to modify the item
    long rowId = adapter.getItemId(position); //Will return the clicked item
    saveItToDB(rowId);
}

以下是适配器的更多代码:

public class DiaryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<MyDiary> fragment_monday;

    public DiaryAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        fragment_monday = new ArrayList<MyDiary>();
        getdata();

        ListView list = getListView();
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    new EditListItemDialog(Monday.this, null, position).show();

                return true;       
            }
        });

    }

    public void getdata(){
        Cursor c = dba.getdiaries();
        startManagingCursor(c);
        if(c.moveToFirst()){
            do{
                String title =
                        c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                String content =
                        c.getString(c.getColumnIndex(Constants.CONTENT_NAME));

                MyDiary temp = new MyDiary(title,content);
                fragment_monday.add(temp);
            } while(c.moveToNext());
        }

    }



    @Override
    public int getCount() {return fragment_monday.size();}
    public MyDiary getItem(int i) {return fragment_monday.get(i);}
    public long getItemId(int i) {return i;}
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        final ViewHolder holder;

        View v = arg1;
        if ((v == null) || (v.getTag() == null)) {
            v = mInflater.inflate(R.layout.diaryrow,  null);
            holder = new ViewHolder();
            holder.mTitle = (TextView)v.findViewById(R.id.name);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.mdiary = getItem(arg0);
        holder.mTitle.setText(holder.mdiary.title);


        v.setTag(holder);

        return v;


    }

    public class ViewHolder {
        MyDiary mdiary;
        TextView mTitle;


    }

}

这是MyDB文件:

public class MyDB {
    private static final String TABLE_NAME = null;
    private static final String KEY_ID = null;
    private SQLiteDatabase db;
    private final Context context;
    private final MyDBhelper dbhelper;



    // Initializes MyDBHelper instance
    public MyDB(Context c){

        context = c;
        dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
                                        Constants.DATABASE_VERSION);

    }


    // Closes the database connection
    public void close()
    {
        db.close();

    }

    // Initializes a SQLiteDatabase instance using MyDBhelper
    public void open() throws SQLiteException
    {

        try {
            db = dbhelper.getWritableDatabase();
        } catch(SQLiteException ex) {
            Log.v("Open database exception caught", ex.getMessage());
            db = dbhelper.getReadableDatabase();
        }

    }

    // updates a diary entry (existing row)
        public boolean updateDiaryEntry(String title, long rowId)
        {

            ContentValues newValue = new ContentValues();
            newValue.put(Constants.TITLE_NAME, title);

            return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;

        }

    // Reads the diary entries from database, saves them in a Cursor class and returns it from the method
    public Cursor getdiaries()
    {
        Cursor c = db.query(Constants.TABLE_NAME, null, null,
                        null, null, null, null);
        return c;
    }


}

我认为代码已经获得了行ID,我可以使用rowId而不是数字来更新被点击的行,但不知道哪个行没有得到更新。谁能指出我的错误?感谢。

1 个答案:

答案 0 :(得分:0)

您必须以预先准备的语句样式提供args。

取自BARACUS Android框架的BaseDao:

 db.update(rowMapper.getAffectedTable()
         , cv
         , getIdField() + "= ?"
         , new String[]{item.getId().toString()});

所以按照你的意思:

db.update(Constants.TABLE_NAME
        , newValue
        , Constants.KEY_ID + "= ?"
        , new String[]{ rowId.toString() })