首页 > 解决方案 > 如何在 SQliteopenhelper 中更新项目位置

问题描述

我用 SQliteopenhelper 保存我的 recyclerview 。我使用 Itemtouchhelper,我可以在回收站视图上使用 itemtouchhelper 更改项目位置,但我无法更新我的数据库中的位置我该怎么办?

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

}

数据库助手.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;
    }



}

适配器.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);



        }
    }
}

那是我的java类。我的活动是todoactivity。我的 SQliteopenhelper 是DatabaseHelper.java。我的适配器是adapter.java。我可以更改我的 recyclerview 上的项目位置,但我无法更新我的数据库上的项目位置。我怎样才能

标签: javaandroidandroid-recyclerviewandroid-sqlite

解决方案


我建议添加一列,其中包含用于订购的位置。

请关闭光标并返回对象列表。您可以使用 读取光标cursor.getString(cursor.getColumnIndex(colName))。在你的助手中覆盖 close 并关闭 writableDatabase 也是一个很好的做法。我自己只打开一次可写数据库(在创建时)并在最后关闭它(在关闭时)。

问候,迈克


推荐阅读