简体   繁体   English

如何在 SQliteopenhelper 中更新项目 position

[英]How can ı update items position in SQliteopenhelper

I save my recyclerview with SQliteopenhelper.我用 SQliteopenhelper 保存了我的 recyclerview。 I use Itemtouchhelper and I can change items positions with itemtouchhelper on recycler view but I cant update positions on my database How Can I?我使用 Itemtouchhelper,我可以在回收站视图上使用 itemtouchhelper 更改项目位置,但我无法更新我的数据库中的位置我该怎么办?

todoactivity.java todoactivity.java

public class todoactivity extends AppCompatActivity    {
    TextView title;
    ImageButton gorevo;
    RecyclerView recyclerView;
    List<String>Listsx = new ArrayList<>();
    TodoActivityAdpter adapterx;
    DatabaseHelper4 myDBxxx;
    TextView textView;
    CheckBox checkBox;
    long id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todoactivity);
        recyclerView=findViewById(R.id.recyclerviewxx);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        adapterx=new TodoActivityAdpter(Listsx);
        recyclerView.setAdapter(adapterx);
        title=findViewById(R.id.titlex);
        textView=findViewById(R.id.text_viewx);
        gorevo = findViewById(R.id.gorevo);

        myDBxxx = new DatabaseHelper4(this);

        Cursor datax = myDBxxx.getListContents();
        if(datax.getCount() == 0){

        }else{
            while(datax.moveToNext()){
                Listsx.add(datax.getString(1));

                ListAdapter listAdapterx = new ArrayAdapter<>(this,R.layout.todoactivity_item,R.id.textitem,Listsx);
                adapterx.notifyItemInserted(Listsx.size()-1);
            }
        }


        gorevo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(todoactivity.this);
                bottomSheetDialog.setContentView(R.layout.bottomsheetlayout3);
                bottomSheetDialog.show();
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
                EditText editText = bottomSheetDialog.findViewById(R.id.editx);
                Button ekle = bottomSheetDialog.findViewById(R.id.ekle);
                ekle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String text = editText.getText().toString();
                        Listsx.add(text);
                        AddDataxxx(text);
                        adapterx.notifyItemInserted(Listsx.size()-1);
                        bottomSheetDialog.hide();
                        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
                    }
                });

            }
        });
        back=findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(todoactivity.this, pomodoroscreen.class);
                startActivity(i);
                overridePendingTransition(0,0);
            }
        });



  
        ItemTouchHelper.SimpleCallback move = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END , 0) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
                int toPosition = target.getAdapterPosition();
                Collections.swap(Listsx,fromPosition,toPosition);
                recyclerView.getAdapter().notifyItemMoved(fromPosition,toPosition);

                // I want update items position on my SQliteOpenhelper in there

                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

            }
        };
        
        ItemTouchHelper itemTouchHelperx = new ItemTouchHelper(move);
        itemTouchHelperx.attachToRecyclerView(recyclerView);
    }

    public void AddDataxxx(String newEntry) {

        boolean insertDatax = myDBxxx.addDataxxx(newEntry);
    }

}

DatabaseHelper.java数据库助手.java

public class DatabaseHelper4 extends SQLiteOpenHelper {

    public static final String DATABASE_NAME4 = "mylistxxx.db";
    public static final String TABLE_NAME4 = "mylist_dataxxx";
    public static final String COL14 = "iDxxx";
    public static final String COL24 = "ITEM1xxx";


    public DatabaseHelper4(Context context) {
        super(context, DATABASE_NAME4, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase dbxxx) {
        String createTable = "CREATE TABLE " + TABLE_NAME4 + " (iDxxx INTEGER PRIMARY KEY AUTOINCREMENT, " +
                " ITEM1xxx TEXT)";
        dbxxx.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase dbxxx, int oldVersion, int newVersion) {
        dbxxx.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME4);
        onCreate(dbxxx);
    }

    public boolean addDataxxx(String textt) {
        SQLiteDatabase dbxxx = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL24, textt);

        long result = dbxxx.insert(TABLE_NAME4, null, contentValues);

        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

    public Cursor getListContents() {
        SQLiteDatabase dbxxx = this.getWritableDatabase();
        Cursor dataxxx = dbxxx.rawQuery("SELECT * FROM " + TABLE_NAME4, null);
        return dataxxx;
    }



}

Adapter.java适配器.java

public class TodoActivityAdpter extends RecyclerView.Adapter<TodoActivityAdpter.Holder> {



    List<String>Listsx;
    public TodoActivityAdpter(List<String>itemxxx){
        this.Listsx = itemxxx;
    }
    @NonNull
    @Override
    public TodoActivityAdpter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.todoactivity_item,parent,false);
        Holder holder = new Holder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(@NonNull TodoActivityAdpter.Holder holder, int position) {
        holder.textView.setText(Listsx.get(position));


        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.checkBox.isChecked()) {


                    holder.textView.setTextColor(view.getResources().getColor(R.color.grey));
                } else {
                    holder.textView.setTextColor(view.getResources().getColor(R.color.Color_black));
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return Listsx.size();
    }

    public class Holder extends RecyclerView.ViewHolder {

        CheckBox checkBox;
        TextView textView;

        List<String>Listsx;
        RecyclerView recyclerView;
        Context mContext;
        public Holder(View view) {
            super(view);

            textView=view.findViewById(R.id.text_viewx);
            checkBox=view.findViewById(R.id.checkbox);
            recyclerView=view.findViewById(R.id.recyclerviewxx);



        }
    }
}

Thats my java classes.这就是我的 java 课程。 My activity is todoactivity .我的活动是todoactivity My SQliteopenhelper is DatabaseHelper.java .我的 SQliteopenhelper 是DatabaseHelper.java My Adapter is adapter.java .我的适配器是adapter.java I can change items position on my recyclerview but ı cant update items position on my database.我可以在我的 recyclerview 上更改项目 position 但我无法在我的数据库上更新项目 position。 How can I我怎样才能

I suggest adding a column with the position to use for ordering.我建议添加一个带有 position 的列以用于订购。

Please close the cursor and return a list of objects.请关闭 cursor 并返回对象列表。 You can read your cursor using cursor.getString(cursor.getColumnIndex(colName)) .您可以使用cursor.getString(cursor.getColumnIndex(colName))阅读您的 cursor 。 Overriding close in your helper and closing the writableDatabase is also good practise.在你的助手中覆盖 close 并关闭 writableDatabase 也是一个很好的做法。 I myself open the writableDatabase only once (upon create) and close it at the end (upon close).我自己只打开一次可写数据库(在创建时)并在最后关闭它(在关闭时)。

Regards, Mike问候,迈克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM