I将一些数据上传到sqlite,我想通过按下按钮从回收视图中删除每个数据。那么我该如何做到这一点呢?我想将id从recyclerview获取到mainactivity,然后从sqlite中删除数据并更新reyclerview。我怎样才能做到这一点?
主要活动
public class SelectedProblems extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selecte_problem_drawal);
mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); // Instantiate Recyclerview
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
selectedProblemsAdapter = new SelectedProblemsAdapter(this, getAllItems(), listener);
mRecyclerView.setAdapter(selectedProblemsAdapter);
Button remove = findviewbyid(R.id.remove);
remove.setOnClickListener(new View.OnClickListener() {
}
}
选择的问题适配器
public SelectedProblemsAdapter(Context context, Cursor cursor, OnItemClick listener) {
this.context = context;
this.cursor = cursor;
listener = listener;
}
@Override
public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
return new SelectedProblemsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SelectedProblemsViewHolder holder, final int position) {
if (this.cursor != null){
}
}
@Override
public int getItemCount() {
return (null != cursor ? cursor.getCount(): 0);
}
public void update(Cursor cursor) {
this.cursor = cursor;
notifyDataSetChanged();
}
class SelectedProblemsViewHolder extends RecyclerView.ViewHolder{
TextView selectedProblems, selectedProblemPrice, selectedProblemTotal;
Button remove_problem_from_cart;
public SelectedProblemsViewHolder(View itemView) {
super(itemView);
selectedProblems = itemView.findViewById(R.id.selected_problems);
selectedProblemPrice = itemView.findViewById(R.id.selected_problem_price);
selectedProblemTotal = itemView.findViewById(R.id.selectedProblemTotal);
remove_problem_from_cart = itemView.findViewById(R.id.remove_problem_from_cart);
}
}
}
主活动
public class SelectedProblems extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selecte_problem_drawal);
mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); // Instantiate Recyclerview
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
selectedProblemsAdapter = new SelectedProblemsAdapter(this, getAllItems(), listener);
mRecyclerView.setAdapter(selectedProblemsAdapter);
Button remove = findviewbyid(R.id.remove);
remove.setOnClickListener(new View.OnClickListener() {
}
}
选择的问题适配器
public SelectedProblemsAdapter(Context context, Cursor cursor, OnItemClick listener) {
this.context = context;
this.cursor = cursor;
listener = listener;
}
@Override
public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
return new SelectedProblemsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SelectedProblemsViewHolder holder, final int position) {
if (this.cursor != null){
// it will call your interface method which is implemented in Activity
holder.yourView_name.setOnClickListener(new View.OnClickListener() {
listener.yourmethod();
}
}
}
@Override
public int getItemCount() {
return (null != cursor ? cursor.getCount(): 0);
}
public void update(Cursor cursor) {
this.cursor = cursor;
notifyDataSetChanged();
}
class SelectedProblemsViewHolder extends RecyclerView.ViewHolder{
TextView selectedProblems, selectedProblemPrice, selectedProblemTotal;
Button remove_problem_from_cart;
public SelectedProblemsViewHolder(View itemView) {
super(itemView);
selectedProblems = itemView.findViewById(R.id.selected_problems);
selectedProblemPrice = itemView.findViewById(R.id.selected_problem_price);
selectedProblemTotal = itemView.findViewById(R.id.selectedProblemTotal);
remove_problem_from_cart = itemView.findViewById(R.id.remove_problem_from_cart);
}
}
}
您可以通过实现一个接口来实现它。
步骤1:创建一个带有抽象函数的接口,该函数将数据类型作为其参数。
public interface OnDataItemClickListener {
void onItemClick(YourItem yourItem);
}
步骤2:使活动实现接口并覆盖其方法。
public class SelectedProblems extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selecte_problem_drawal);
mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); //
//Instantiate Recyclerview
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
selectedProblemsAdapter = new SelectedProblemsAdapter(this, getAllItems());
mRecyclerView.setAdapter(selectedProblemsAdapter);
}
@Override
public void onItemClick(YourItem yourItem) {
}
步骤3:通过适配器的构造函数传递接口。
//Global variable of the interface
private onDataItemClickListener onclickListner;
// while instatiating the adapter
selectedProblemsAdapter = new SelectedProblemsAdapter(this, getAllItems(), onClickListener);
步骤4:在onBindViewHolder方法中,使用接口实例(通过构造函数接收(来调用方法,这将有助于将数据传递给主活动/活动。
public SelectedProblemsAdapter(Context context, Cursor cursor, OnDataItemClickListener listener) {
this.context = context;
this.cursor = cursor;
listener = listener;
}
@Override
public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
return new SelectedProblemsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SelectedProblemsViewHolder holder, final int position) {
if (this.cursor != null){
holder.yourView.setOnClickListener(new View.OnClickListener() {
// onItemClick is the abstract function present in the interface.
listener.onItemClicked(" your data item ");
// here the data item/position etc, which you want to delete will be passed on to the main acticity.
}
}
}
@Override
public int getItemCount() {
return (null != cursor ? cursor.getCount(): 0);
}
public void update(Cursor cursor) {
this.cursor = cursor;
notifyDataSetChanged();
}
class SelectedProblemsViewHolder extends RecyclerView.ViewHolder{
TextView selectedProblems, selectedProblemPrice, selectedProblemTotal;
Button remove_problem_from_cart;
public SelectedProblemsViewHolder(View itemView) {
super(itemView);
selectedProblems = itemView.findViewById(R.id.selected_problems);
selectedProblemPrice = itemView.findViewById(R.id.selected_problem_price);
selectedProblemTotal = itemView.findViewById(R.id.selectedProblemTotal);
remove_problem_from_cart = itemView.findViewById(R.id.remove_problem_from_cart);
}
}
}
步骤5:最后在主活动中通过重写的接口方法编写代码来删除对象。
@Override
public void onItemClick(YourItem yourItem) {
yourItem.delete();
// you can use any number of functions to perform with your dataItem here.
selectedProblemsAdapter.update()
//call your update function here to change the list in recycler view.
}
注意:如果您使用的是视图模型和实时数据,则不需要编写notifyDataSetChanged((函数。数据会自动更新。
这将帮助您删除或对从适配器传递到主活动的数据执行任意数量的操作。