如何在 Android Studio 中同时从 recyclerview 和数据库中删除数据? [关闭]
Posted
技术标签:
【中文标题】如何在 Android Studio 中同时从 recyclerview 和数据库中删除数据? [关闭]【英文标题】:How can I delete data from recyclerview and database at the same time in Android Studio? [closed] 【发布时间】:2022-01-17 01:57:03 【问题描述】:我有一个 recyclerview,我在 recyclerview 中显示城市名称,当我在 recyclerview 中长按城市名称时,我想删除我点击的城市。
除第一个和最后一个城市外,删除成功。当我想从我的 recyclerview 中删除最后一个城市和第一个城市时,它正在删除,但它没有从我的 sql 数据库中删除,它显示我的 toast 消息“出了点问题”。
如何从数据库中删除我想删除的城市?
“空对象引用上的Adapter.notifyDataSetChanged()'”
我的适配器类
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>
ArrayList<City> arrayList;
Context context;
SQLiteDatabase db ;
Cursor cursor;
int id;
public Adapter(ArrayList<City> arrayList ,Context context )
this.arrayList = arrayList;
this.context = context;
db = context.openOrCreateDatabase("City",MODE_PRIVATE,null);
Intent intent = ((Activity) context).getIntent();
id = intent.getIntExtra("citId", 0);
@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);
/*when i long click it does the deletion here*/
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());
cursor = db.rawQuery("select * from city where id=?",new String[]String.valueOf(id));
Result(cursor);
).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);
);
private void Result(Cursor cursor)
if(cursor.getCount() > 0)
db.delete("City","id=?",new String[]String.valueOf(id));
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);
我的 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();// <--- There is an error in this code
cursor.close();
catch (Exception e )
e.printStackTrace();
/*---------------------- set recyclerview-----------------------------*/
cityadapter = new Adapter(cityArrayList,this);
recyclerView.setAdapter(cityadapter);
recyclerView.setLayoutManager(new LinearLayoutManager(cities.this));
/*------- 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);
【问题讨论】:
【参考方案1】:您的问题是错误点的 cityAdapter
尚未实例化,因此为空。你稍后实例化它:-
cityadapter = new Adapter(cityArrayList,this);
您可以在例如之前移动实例化
private void SQLGet_Data()
cityadapter = new Adapter(cityArrayList,this);
try
....
【讨论】:
以上是关于如何在 Android Studio 中同时从 recyclerview 和数据库中删除数据? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Android Studio 将我所有的引用从 R 更改为 android.R?