【问题标题】:How to get id data from database deleting - setOnLongClickListener ERROR如何从数据库删除中获取 id 数据 - setOnLongClickListener ERROR
【发布时间】:2022-01-17 20:05:48
【问题描述】:

我有一个 recyclerview 和 sql lite 数据库。我将城市名称保存在 SQLite 数据库中,并将它们显示在 recyclerview 中。当城市在 recyclerview 中被 setOnclickListener 时,城市的天气就会显示出来,我通过从 sql lite 数据库中获取城市的 id 来做到这一点。

arrayList.get(position).id

它在 setOnClickListener 中有效,但在 setOnLongClickListener 中无效

我想删除recyclerview中的城市名,当我长按但没有删除,因为它不起作用。

logcat中出现这个错误:" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" 或类似的东西

我该如何解决这个问题?

我的适配器类

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>  {

ArrayList<City> arrayList;
Context context;
SQLiteDatabase db ;
Cursor cursor;
int dbId;



public Adapter(ArrayList<City> arrayList ,Context context ){
    this.arrayList = arrayList;
    this.context = context;

    db = context.openOrCreateDatabase("City",MODE_PRIVATE,null);


}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    RecyclerviewRowBinding recyclerviewRowBinding = 
    RecyclerviewRowBinding.inflate(LayoutInflater.from(parent.getContext()),
    parent,
    false);
    return new MyViewHolder(recyclerviewRowBinding);

}

@Override
public void onBindViewHolder(@NonNull Adapter.MyViewHolder holder, int position) {

    holder.binding.MytxtCities.setText(arrayList.get(position).cityName);


    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setIcon(R.drawable.warningicon);
            builder.setMessage("Are you sure that you want to delete "+arrayList.get(position).cityName);
            builder.setPositiveButton("Ok", (dialog, which) -> {

                arrayList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position,arrayList.size());

                dbId = arrayList.get(position).id; // <---the error is in this code
                System.out.println("dbId"+dbId);

                //db.execSQL("DELETE FROM city WHERE id ='" + arrayList.get(position).id + "'");


                cursor = db.rawQuery("SELECT * FROM city WHERE id=?",new String[]{String.valueOf(dbId)});
                Result(cursor);



            }).setNegativeButton("Cancel", (dialog, which) -> {

                //doing nothing
            }).show();


            return true;
        }


    });


    holder.itemView.setOnClickListener(v -> {

        Intent intent = new Intent(holder.itemView.getContext(),MainActivity.class);
        intent.putExtra("citId",arrayList.get(position).id); // <-- it works very well here and sends with intent
        holder.itemView.getContext().startActivity(intent);
    });
}


private void Result(Cursor cursor){

    if(cursor.getCount() > 0){

        db.delete("city","id=?",new String[]{String.valueOf(dbId)});
        notifyDataSetChanged();
    }
    else{
        Toast.makeText(context,"something went wrong !",Toast.LENGTH_LONG).show();
    }


}


@Override
public int getItemCount() {
    return arrayList.size();
}



public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView Mytxt_cities;
    private RecyclerviewRowBinding binding;

    public MyViewHolder(@NonNull RecyclerviewRowBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
        Mytxt_cities = itemView.findViewById(R.id.Mytxt_cities);



    }


 }
 }

【问题讨论】:

    标签: java android android-studio android-recyclerview onlongclicklistener


    【解决方案1】:

    你的代码里有

    arrayList.remove(position);
    

    下面几行

    dbId = arrayList.get(position).id; // <---the error is in this code
    

    remove 调用后,您的数组列表为空,但您仍在尝试在position 获取此项目,因此您正在获取IndexOutOfBoundsException: Index: 0, Size: 0 - 您正在尝试在position = 0 获取项目并且数组为空

    在从数组中删除之前尝试移动获取dbId

                dbId = arrayList.get(position).id;
                arrayList.remove(position);
                notifyItemRemoved(position);
                //notifyItemRangeChanged(position,arrayList.size()); // this line is unnecessary
    

    【讨论】:

    • 谢谢。非常合乎逻辑。我按照你说的解决了这个问题。我这样做了 1 - dbId = arrayList.get(position).id; 2- arrayList.remove(位置);
    猜你喜欢
    • 1970-01-01
    • 2015-09-23
    • 2021-11-09
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2017-12-24
    相关资源
    最近更新 更多