更新SQL行与OnItemLongClick不起作用 [英] Updating SQL row with OnItemLongClick doesn't work

查看:98
本文介绍了更新SQL行与OnItemLongClick不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以请看看我的code和告诉我在哪里,我错了吗?我不明白的错误,可惜就是长pressed并提供不被更新,除非我指定确切的行数的新值的行。我需要能够更新被点击的行。我尝试了一切,到今天我没能得到任何帮助。我初学Android开发。

下面是的MyDB的code:

 公共类的MyDB {
    私有静态最后弦乐TABLE_NAME = NULL;
    私有静态最后弦乐KEY_ID = NULL;
    私人SQLiteDatabase分贝;
    私人最终上下文的背景下;
    私人最终MyDBhelper dbhelper;



    //初始化MyDBHelper实例
    公众的MyDB(上下文C){

        上下文= C;
        dbhelper =新MyDBhelper(背景下,Constants.DATABASE_NAME,空,
                                        Constants.DATABASE_VERSION);

    }


    //关闭数据库连接
    公共无效的close()
    {
        db.close();

    }

    //初始化使用MyDBhelper一个SQLiteDatabase实例
    公共无效的open()抛出SQLiteException
    {

        尝试 {
            DB = dbhelper.getWritableDatabase();
        }赶上(SQLiteException前){
        Log.v(打开数据库异常捕获,ex.getMessage());
            DB = dbhelper.getReadableDatabase();
    }

    }

    //更新日记条目(现有行)
        公共布尔updateDiaryEntry(字符串标题,长ROWID)
        {

            ContentValues​​为newValue =新ContentValues​​();
            newValue.put(Constants.TITLE_NAME,职称);

            db.beginTransaction();
            db.setTransactionSuccessful();
            db.endTransaction();
            返回db.update(Constants.TABLE_NAME,为newValue,Constants.KEY_ID +=?,
                新的String [] {Double.valueOf(ROWID)的ToString()})大于0;


        }

    //读取从数据库中的日记,并将它们保存在一个Cursor类和方法返回它
    公共光标getdiaries()
    {
        光标C = db.query(Constants.TABLE_NAME,NULL,NULL,
                        NULL,NULL,NULL,NULL);
        返回℃;
    }


}
 

下面是code的对话框,在这里我要更新该行:

 类EditListItemDialog扩展对话框实现View.OnClickListener {
    的MyDB DBA;
    私人查看EDITTEXT;
    私人DiaryAdapter适配器;
    私人SQLiteDatabase分贝;

    公共EditListItemDialog(上下文的背景下){
        超(上下文);
        DBA =新的MyDB(上下文);
    }

    @覆盖
    保护无效的onCreate(包savedInstanceState){

        super.onCreate(savedInstanceState);
        的setContentView(R.layout.edit_text_dialog); //这里是你的XML具有的EditText和确定和取消按钮
        查看btnOk = findViewById(R.id.button_ok);
        EDITTEXT = findViewById(R.id.edit_text);
        btnOk.setOnClickListener(本);

        dba.open();
    }

    私人列表<字符串> fragment_monday;
    专用长ROWID;


    公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
        //定位是项目的数量点击
        //您可以使用您的适配器来修改项目
        长ROWID = adapter.getItemId(位置); //将返回点击的项目
        saveItToDB(ROWID);
    }

    公共EditListItemDialog(上下文的背景下,DiaryAdapter适配器,INT位置){
        超(上下文);
        this.fragment_monday =新的ArrayList<字符串>();
        this.adapter =适配器;
        DBA =新的MyDB(上下文);
    }

    @覆盖
    公共无效的onClick(视图v){

        fragment_monday.add(((TextView中)V).getText()的toString()); //这里是你的更新(或不更新)文本
        //公共无效notifyDataSetChanged();
        解雇();
        尝试 {
            saveItToDB(ROWID);
        }赶上(例外五){
            e.printStackTrace();
        }
    }

    私人无效saveItToDB(长ROWID){
        dba.open();
        dba.updateDiaryEntry(((TextView中)EDITTEXT).getText()的toString(),ROWID);
        dba.close();
        ((TextView中)EDITTEXT).setText();
    }
}
 

这里是日记适配器:

 公共类DiaryAdapter扩展了BaseAdapter {
        私人LayoutInflater mInflater;
        私人的ArrayList< MyDiary> fragment_monday;

        公共DiaryAdapter(上下文的背景下){
            mInflater = LayoutInflater.from(上下文);
            fragment_monday =新的ArrayList< MyDiary>();
            的getData();

            ListView控件列表= getListView();
            list.setOnItemLongClickListener(新AdapterView.OnItemLongClickListener(){
                    公共布尔onItemLongClick(适配器视图<>母公司视图中查看,INT位置,长的id){
                        新EditListItemDialog(Monday.this,空,位置).show();

                    返回true;
                }
            });

        }

        公共无效的getData(){
            光标C = dba.getdiaries();
            startManagingCursor(C);
            如果(c.moveToFirst()){
                做{
                    字符串标题=
                        c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                    字符串的内容=
                        c.getString(c.getColumnIndex(Constants.CONTENT_NAME));

                    MyDiary临时=新MyDiary(标题,内容);
                    fragment_monday.add(临时);
                }而(c.moveToNext());
            }

        }



        @覆盖
        公众诠释getCount将(){返​​回fragment_monday.size();}
        公共MyDiary的getItem(int i)以{返回fragment_monday.get(ⅰ);}
        众长getItemId(int i)以{回报我;}
        公共查看getView(INT为arg0,查看ARG1,ARG2的ViewGroup){
            最后ViewHolder持有人;

            视图V = ARG1;
            如果((V == NULL)||(v.getTag()== NULL)){
                V = mInflater.inflate(R.layout.diaryrow,NULL);
                持有人=新ViewHolder();
                holder.mTitle =(TextView中)v.findViewById(R.id.name);

                v.setTag(保持器);
            } 其他 {
                支架=(ViewHolder)v.getTag();
            }

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


            v.setTag(保持器);

            返回伏;


        }

        公共类ViewHolder {
            MyDiary mdiary;
            TextView的mTitle;


        }

}
 

解决方案

我发现第一个问题,在您的DB类 更新这个方法

 公开光标getdiaries()
{
    光标C = db.query(Constants.TABLE_NAME,NULL,NULL,
                    NULL,NULL,NULL,NULL);
    返回℃;
}
 

 公开光标getdiaries()
{
    光标C = db.query(Constants.TABLE_NAME,新的String [] {Constants.TITLE_NAME,Constants.CONTENT_NAME},空,
                    NULL,NULL,NULL,NULL);
    返回℃;
}
 

Can anybody please have a look at my code and tell me where I am wrong? I don't get errors, unfortunately the row that is long pressed and new value is provided is not being updated unless I specify exact row number. I need to be able to update the row that is clicked. I tried everything and up to today I didn't manage to get any help. I am beginner in Android development.

Here is the code of 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);

            db.beginTransaction();
            db.setTransactionSuccessful();
            db.endTransaction();
            return db.update(Constants.TABLE_NAME , newValue , Constants.KEY_ID + "= ?" , 
                new String[]{ Double.valueOf(rowId).toString() })>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;
    }


}

Here is the code with the dialog, where I should update the row:

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("");
    }
}

And here is the Diary Adapter:

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;


        }

}

解决方案

i found first problem at your DB class update this method

 public Cursor getdiaries()
{
    Cursor c = db.query(Constants.TABLE_NAME, null, null,
                    null, null, null, null);
    return c;
}

To

 public Cursor getdiaries()
{
    Cursor c = db.query(Constants.TABLE_NAME, new String[]{Constants.TITLE_NAME  ,Constants.CONTENT_NAME}, null,
                    null, null, null, null);
    return c;
}

这篇关于更新SQL行与OnItemLongClick不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆