gpt4 book ai didi

java - 单击按钮时如何将数据从适配器传递到主 Activity

转载 作者:行者123 更新时间:2023-12-01 17:53:40 24 4
gpt4 key购买 nike

我将一些数据上传到 sqlite,我想通过按按钮从 recyclerview 中删除每个数据。那么我怎样才能做到这一点呢?我想从 recyclerview 获取 id 到 mainactivity,然后从 sqlite 中删除数据并更新 reyclerview。我怎样才能实现这个目标?

主要 Activity

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() {

}
}

SelectedProblemsAdapter

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

}
}
}

最佳答案

主要 Activity

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() {

}
}

SelectedProblemsAdapter

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

}
}
}

关于java - 单击按钮时如何将数据从适配器传递到主 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60755394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com