如何将数据从RecyclerAdapter类发送到Main Activity

时间:2020-04-08 08:47:40

标签: java android firebase google-cloud-firestore

我正在扩展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);
                }
            }
        });

通过接口传递此密码: CategoryTextView.setText(category.getCategory_name());

2 个答案:

答案 0 :(得分:0)

只需像这样扩展您的监听器

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

您可以使用相同的侦听器,并根据需要扩展它。另外,您可以将类别数据保存在具有所有者位置的列表中,并在以后使用时使用(也许在单击侦听器中)

答案 1 :(得分:0)

在相同的界面中实现。

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