如何修复 Recyclerview 项目删除显示错误

Posted

技术标签:

【中文标题】如何修复 Recyclerview 项目删除显示错误【英文标题】:How can I fix Recyclerview item deletion display error 【发布时间】:2022-01-16 09:39:27 【问题描述】:

我有一个回收站视图。 recyclerview中有城市名称,当我长按时,我想从recyclerview中删除它。我在适配器类中写了一些代码。当我点击城市名称时,我可以删除它们,但是当我再次查看recyclerview时,我删除的城市名称又出现了。我该如何解决这个问题?

我的适配器类

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

ArrayList<City> arrayList;
Context context;

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



@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", new DialogInterface.OnClickListener() 
                @Override
                public void onClick(DialogInterface dialog, int which) 
                    arrayList.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position,arrayList.size());
                
            ).setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                @Override
                public void onClick(DialogInterface dialog, int which) 

                
            ).show();

            return true;
        
    );


    holder.itemView.setOnClickListener(v -> 
        Intent intent = new Intent(holder.itemView.getContext(),MainActivity.class);
        intent.putExtra("citId",arrayList.get(position).id);
        holder.itemView.getContext().startActivity(intent);
    );


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



    



我的 recyclerview 类是城市类

public class cities extends AppCompatActivity 

RecyclerView recyclerView ;
ArrayList<City> cityArrayList;
Adapter cityadapter;
ImageView cities_back_icon;



public void init()

    cities_back_icon = findViewById(R.id.Id_cities_back_icon);
    cities_back_icon_click_register();

    cityArrayList = new ArrayList<>();
    recyclerView = findViewById(R.id.recyclerview_id);



    SQLGet_Data();



@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cities);
    init();




private void cities_back_icon_click_register()

    cities_back_icon.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent intent = new Intent(cities.this, MainActivity.class);
            startActivity(intent);
        
    );


private void SQLGet_Data()
    try 

        SQLiteDatabase sqLiteDatabase = this.openOrCreateDatabase("City",MODE_PRIVATE,null);
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT*FROM city",null);
        int idIx = cursor.getColumnIndex("id");
        int nameIx = cursor.getColumnIndex("cityname");

        while(cursor.moveToNext())
            String cityname = cursor.getString(nameIx);
            int id = cursor.getInt(idIx);
            City city = new City(cityname,id);
            cityArrayList.add(city);
        
        cityadapter.notifyDataSetChanged();
        cursor.close();
    

    catch (Exception e )
       e.printStackTrace();
    
                 /*---------------------- set recyclerview-----------------------------*/

    recyclerView.setLayoutManager(new LinearLayoutManager(cities.this));
    cityadapter = new Adapter(cityArrayList,this);
    recyclerView.setAdapter(cityadapter);





/*--------------------------We drew a line between the data in the recyclerview------------------------------------*/

    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
    Drawable drawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.custom_divider);
    dividerItemDecoration.setDrawable(drawable);
    recyclerView.addItemDecoration(dividerItemDecoration);


【问题讨论】:

您正在从 SQL 数据库中检索数据,但仅从列表中删除该项目。要完全删除该项目以使其不再显示,您还必须将其从数据库中删除。 【参考方案1】:

问题是您只是从适配器内的ArrayList 中删除城市。但是这些城市仍然保留在您的 SQL 数据库中。然后,当您重新启动 Activity 时,您的 ArrayList 将使用您的 SQL 数据库的数据创建,这些已删除的城市仍然存在。

要始终删除城市,您不仅需要删除适配器中的城市,还需要删除 SQL 数据库中的城市。

【讨论】:

好的。我会试试这个谢谢你 谢谢你的工作

以上是关于如何修复 Recyclerview 项目删除显示错误的主要内容,如果未能解决你的问题,请参考以下文章

将 RecyclerView 可见性从 Gone 更改为 Visible,在显示新添加的项目之前暂时错误地显示先前删除的项目

如何从 recyclerview 和 sqlite 中删除项目? [复制]

如何在 RecyclerView 项目更改上触发 MainActivity 映射修改

从 RecyclerView 列表中删除某些为空的项目?

删除 RecyclerView android中项目之间的间距

Android Kotlin:如何从 recyclerview 中成功删除项目、调用 notifyItemRangeChanged() 和动画