简体   繁体   English

如何将数据从 RecyclerAdapter class 发送到主活动

[英]How to send data from RecyclerAdapter class to Main Activity

I am extending FirestoreAdapter Class to populate the data from firestore.How can i pass the on item selected Tittle to the main Class.我正在扩展 FirestoreAdapter Class 以填充来自 firestore 的数据。如何将选中的项目标题传递给主 Class。 I using the interface to send the document ID of the selected item with that how can i pass the selected tittle ie TextView of the item clicked.Or should i use Intent.put extra to pass it to main Activity.Thanks in Advance My Code:我使用界面发送所选项目的文档 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);
                }
            }
        });

to pass this one with the interface: CategoryTextView.setText(category.getCategory_name());用接口传递这个: CategoryTextView.setText(category.getCategory_name());

Just extend your Listener like this像这样扩展你的监听器

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

and

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

You can use the same listener and extend it how you like.您可以使用相同的侦听器并根据需要扩展它。 Also, you can save your category data in list with holder position and use it later, when you like (maybe in click listener)此外,您可以使用持有人 position 将类别数据保存在列表中,然后在您喜欢时使用它(也许在点击监听器中)

Implemented in the same interface..在同一个接口中实现..

public interface OnItemSelectedListener {
    void OnItemSelected(DocumentSnapshot item , String Cat_tittle);

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从活动传递到 recyclerAdapter - Android - Pass data from activity to recyclerAdapter - Android 如何将类中的数据发送到Android中的Activity - How to send data from a class to an Activity in Android Android-如何将数据从主要活动发送到加载的片段 - Android - How to send data from main activity to loaded fragment 如何将数据从线程发送到主要活动 - how to send data from thread to main activity java android 如何从 RecyclerAdapter 获取数据? - how can I get data from RecyclerAdapter? 如何将数据从 ViewModel 内的线程发送到主 Activity/Fragment? - How to send data from thread inside a ViewModel to main Activity/Fragment? 如何将数据从异步类传递到主要活动类 - How to pass data from Async Class to Main Activity class 将实时数据从主要活动发送到片段 - Send live data from Main Activity to Fragment 如何从广播接收器向主要活动发送消息 - How to send message to main activity from broadcastreceiver How to Properly Transfer Data from a Model Class to a RecyclerAdapter Class Using an ArrayList (Data Retrieval from Firebase Realtime Database) - How to Properly Transfer Data from a Model Class to a RecyclerAdapter Class Using an ArrayList (Data Retrieval from Firebase Realtime Database)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM