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

huangapple go评论88阅读模式
英文:

How to send data from RecyclerAdapter class to Main Activity

问题

我正在扩展FirestoreAdapter类以从Firestore填充数据。我如何将所选项目的标题传递给主类?我正在使用接口将所选项目的文档ID发送过去,那么如何传递所选的标题,即所点击项目的TextView?或者我应该使用Intent.putExtra将其传递给主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 . I using the interface to send the document ID of the selected item with that how can i pass the selected tittle i.e TextView of the item clicked.Or should i use Intent.put extra to pass it to main Activity.Thanks in Advance
My Code:

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

答案1

得分: 0

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

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

以及

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

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

英文:

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)

答案2

得分: 0

Sure, here's the translated code portion:

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

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

huangapple
  • 本文由 发表于 2020年4月8日 16:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61096726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定