3

Edited: Don't suggest Interface, please.
I have an Activity with a RecyclerView inside. I want post an event from Adapter to Activity by otto, when Adapter will be created. I used 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 Log doesn't show up?

Ssisakhti
  • 96
  • 1
  • 5

4 Answers4

1

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

Shubhang Malviya
  • 1,525
  • 11
  • 17
0
"I want post an event from Adapter to Activity by otto, when Adapter will be created"

You are posting the event in the onClick(View view) method. So it will only post the event when that method is triggered. Move it out of this method if you want to post the event when the adapter is created.

AncientMethods
  • 256
  • 1
  • 9
  • Sorry about that, i missed it because of clean up code. I edited my code. please look at it again. – Ssisakhti Jan 15 '15 at 15:26
  • you are still posting the event in 'setTotalFactor()' which is triggered by the 'onclick' method. Move the event post method onto the Viewholder constructor. – AncientMethods Jan 15 '15 at 18:00
  • After edit, I called setTotalFactor() in ShoppingCardAdapter constructor. am I wrong? – Ssisakhti Jan 18 '15 at 08:16
0

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

To subscribe on the Activity: get a bus instance and register the activity on onResume() and unregister on onPause().

To post from the RecyclerView.ViewHolder: get a bus instance in the ViewHolder's constructor (or get it in the adapter's constructor) and post.

Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
0

I feel like it's a bit late, but here is my answer for anyone who faces this kind of problems, I think the mentioned

ShoppingCardActivity

might be a base abstract class which registration of Otto bus in an abstract class has no effect, So you have to register and unregister in your outer activity which is not abstract, this is what I can say based on your code, If there's any more information please let us know

here is a comment from official documentation:

Registering will only find methods on the immediate class type. 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 performance of the library as well as keep your code simple and unambiguous.

Rez
  • 4,501
  • 1
  • 30
  • 27