How to send event from adapter to activity using otto

Edited: Don't suggest an interface, please.
I have an Activity with a RecyclerView inside. I want to post an event from Adapter to Activity from otto when Adapter is created. I used the following code:
ShoppingCardAdapter

public class ShoppingCardAdapter extends  RecyclerView.Adapter<ShoppingCardAdapter.ViewHolder> {

private Activity activity;
private CardBook cardbook;

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView tvBookName;
    public ViewHolder(View v) {
        super(v);
        tvBookName = (TextView) itemView.findViewById(R.id.tv_book_name);
    }

    @Override
    public void onClick(View view) {
                setTotalFactor();
    }

}

public ShoppingCardAdapter(Activity activity, Realm realm) {
    this.activity = activity;
    dataSet = realm.where(CardBook.class).equalTo("userId", userId).findAll();
    setTotalFactor();
}

@Override
public ShoppingCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.shopping_card_item, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    CardBook cardBook = dataSet.get(position);
    holder.tvBookName.setText(cardBook.getName());

}

@Override
public int getItemCount() {
    return dataSet.size();
}

private void setTotalFactor() {
    BusProvider.getInstance().post(new ShoppingCardBooksChangeEvent(dataSet.size(), String.valueOf(dataSet.sum("price"))));
}}

      


in ShoppingCardActivity

    @Subscribe
public void shoppingCardBooksChanged (ShoppingCardBooksChangeEvent shoppingCardBooksChangeEvent) {
    Log.i("OTTO TEST", "come on :)");
}
@Override protected void onResume() {
    super.onResume();
    BusProvider.getInstance().register(this);
    checkBuyValidation();
}

@Override protected void onPause() {
    super.onPause();
    BusProvider.getInstance().unregister(this);
}

      


why isn't the log displayed?

+3


source to share


4 answers


Register your activity class to receive events like BusProvider.getInstance().register(this);



+1


source


"I want post an event from Adapter to Activity by otto, when Adapter will be created"

      



You are posting the event to the method onClick(View view)

. Thus, it will only publish the event after this method has been run. Move it out of this method if you want to post the event when the adapter is created.

0


source


To send a message from the adapter: get the bus instance in the constructor and send the message.

To subscribe to an Activity: get an instance of the bus and register the activity for onResume()

and unregister for onPause()

.

Publish to RecyclerView.ViewHolder: Get the bus instance in the ViewHolder constructor (or get it in the adapter constructor) and publish.

0


source


It seems to me that this is a little late, but here is my answer for anyone who encounters problems like this, I think mentioned

ShoppingCardActivity

could be a base abstract class that does not take into account the registration of the Otto bus in the abstract class, so you need to register and unregister in your outer activity which is not abstract, this is what I can tell based on your code, If there is any additional information please tell us

here is a comment from the official documentation:

Registration will only look for methods for the immediate type of the class. Unlike the Guava event bus, Otto will not traverse the class hierarchy and add methods from base classes or interfaces that are annotated. This is an explicit design decision to improve the performance of the library, and keep your code simple and unambiguous.

0


source







All Articles