更改sqliteopenhelper上的位置

bd1hkmkf  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(171)

我在我的项目中有一个recyclerview,我可以在我的recyclerview上添加项目。我将我的列表存储在 SQlitehelper . 我可以用适配器改变位置,但我不能储存这个。我如何更改项目位置 SQliteOpenhelper ?
我的活动.java

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

    private ArrayList<Integer> idArray;

    private ArrayList<IdVeri> itemList;

    @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);
        idArray=new ArrayList<>();
        myDBxxx.veriTabanıIndexListele(idArray);
        itemList = new ArrayList<IdVeri>();

        if (idArray.size() != 0) {
            for (int i = 0; i < idArray.size(); i++) {
                itemList.add(new IdVeri(idArray.get(i)));
            }
        }
        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 simpleCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

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

                int positionx = viewHolder.getAdapterPosition();
                Listsx.remove(positionx);
                adapterx.notifyItemRemoved(positionx);
                myDBxxx.deleteItem(itemList.get(positionx).getmSilinecekIndex());

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

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

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

    public void AddDataxxx(String newEntry) {

        boolean insertDatax = myDBxxx.addDataxxx(newEntry);
    }

}

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

        }
    }

}

mydatabasehelper

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;
    }
    public void deleteItem(int iDxxx) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_NAME4 + " WHERE " + COL14 + " = " +
                iDxxx);
    }
    public  void  veriTabanıIndexListele(ArrayList<Integer> zikirid){
        SQLiteDatabase db = this.getReadableDatabase();
        String[] sutunlar = {"iDxxx"};
        Cursor cursor = db.query("mylist_dataxxx",sutunlar,null,null,null,null,null);
        int zikiridIx = cursor.getColumnIndex("iDxxx");
        while (cursor.moveToNext()){
            zikirid.add(cursor.getInt(zikiridIx));
        }
        cursor.close();
        db.close();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题