gpt4 book ai didi

java - 如何将数据从 RecyclerAdapter 类发送到主 Activity

转载 作者:行者123 更新时间:2023-12-01 17:32:15 25 4
gpt4 key购买 nike

我正在扩展 FirestoreAdapter 类来填充 firestore 中的数据。我如何将选定的项目 Tittle 传递给主类。我使用界面发送所选项目的文档 ID,如何传递所选标题,即单击的项目的 TextView。或者我应该使用 Intent.put extra 将其传递给主 Activity。提前致谢我的代码:

public class CategoryMainAdapter extends FirestoreAdapter<CategoryMainAdapter.ViewHolder> {

private static final String TAG = "CategoryMainAdapter";

public interface OnItemSelectedListener {
void OnItemSelected(DocumentSnapshot item);

}

private OnItemSelectedListener mListener;


public CategoryMainAdapter(Query query,OnItemSelectedListener listener) {
super(query);
mListener = listener;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(inflater.inflate(R.layout.categories_list,parent,false));
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bind(getSnapshot(position),mListener);
}

class ViewHolder extends RecyclerView.ViewHolder{
ImageView CategoryImageView;
TextView CategoryTextView;

public ViewHolder(@NonNull View itemView) {
super(itemView);
CategoryImageView = itemView.findViewById(R.id.Cateimage_view);
CategoryTextView = itemView.findViewById(R.id.category_name);
}

public void bind(final DocumentSnapshot snapshot, final OnItemSelectedListener mListener) {

MainCategory category = snapshot.toObject(MainCategory.class);
Resources resources = itemView.getResources();

CategoryTextView.setText(category.getCategory_name());
Glide.with(CategoryImageView.getContext())
.load(category.getCategory_url())
.into(CategoryImageView);

//Click Listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null){
mListener.OnItemSelected(snapshot);
}
}
});

通过接口(interface)传递这个:CategoryTextView.setText(category.getCategory_name());

最佳答案

就像这样扩展你的监听器

public interface OnItemSelectedListener {
void OnItemSelected(DocumentSnapshot item);
void OnItemClicked(String textViewText);
}

//Click Listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null){
mListener.OnItemSelected(snapshot);
mListener.OnItemClicked(CategoryTextView.getText());
}
}
});

您可以使用同一个监听器并按照您喜欢的方式扩展它。此外,您可以将类别数据保存在具有持有者位置的列表中,并在以后需要时使用它(也许在点击监听器中)

关于java - 如何将数据从 RecyclerAdapter 类发送到主 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61096726/

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