无法从数据库获取行ID以更新该行

时间:2014-05-04 17:45:47

标签: android mysql sql

我在书中使用了很多关于如何处理数据库并将数据保存到sql的代码。我是一个绝对的初学者。 我现在正在尝试更新长时间点击的行,但到目前为止还没有好处,我找不到从数据库中获取所点击行的正确ID的方法。我了解到我应该将视图中的数据绑定到数据库中的数据,但我不知道如何执行此操作。如果有人能帮助我,我将非常感激。

现在我已经获得了一些代码。这是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;
    }


}

这是我的EditListItemDialog,它扩展了Dialog:

class EditListItemDialog extends Dialog implements View.OnClickListener {
    MyDB dba;
    private View editText;
    private DiaryAdapter adapter;
    private SQLiteDatabase db;

    public EditListItemDialog(Context context) {           
        super(context);
        dba = new MyDB(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
        View btnOk = findViewById(R.id.button_ok);
        editText = findViewById(R.id.edit_text);
        btnOk.setOnClickListener(this);

        dba.open();
    }

    private List<String> fragment_monday;
    private long 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 EditListItemDialog(Context context, DiaryAdapter adapter, int position) {
        super(context);
        this.fragment_monday = new ArrayList<String>();
        this.adapter = adapter;
        dba = new MyDB(context);
    }

    @Override
    public void onClick(View v) {

        fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
        // public void notifyDataSetChanged();
        dismiss();
        try {
            saveItToDB(rowId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

这里是带有日记适配器的类,它扩展了基本适配器:

public class Monday extends ListActivity {


    private SQLiteDatabase db;
    private static final int MyMenu = 0;
    MyDB dba;
    DiaryAdapter myAdapter;

    private class MyDiary{
        public MyDiary(String t, String c){
            title=t;
            content=c;



    }

        public String title;
        public String content;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dba = new MyDB(this);
        dba.open();
        setContentView(R.layout.fragment_monday);





        super.onCreate(savedInstanceState);
        myAdapter = new DiaryAdapter(this);
        this.setListAdapter(myAdapter);
    }



    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;


        }

    }




    /** Called when the user clicks the Edit button */
    public void visitDiary(View view) {
        Intent intent = new Intent(this, Diary.class);
        startActivity(intent);
    }
    /** Called when the user clicks the back button */
    public void visitSchedule(View view) {
        Intent intent = new Intent(this, DisplayScheduleScreen.class);
        startActivity(intent);
    }


}

0 个答案:

没有答案