简体   繁体   中英

How to set Recyclerview adapter onCallBack

On my activity's onCreate(), I initialized the recyclerview

private RecyclerView listView;

private Subscription subscription;

@Override
protected void onCreate(Bundle savedInstanceState) {

    listView = findViewById(R.id.subscription_list);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);

    layoutManager.setOrientation(RecyclerView.VERTICAL);

    listView.setLayoutManager(layoutManager);

    getPurchases(); //runs a service and sets subscription value and calls back onSkuDetails

}


@Override
public void onSkuDetails(List<SkuDetails> skuDetails) {

    ListCardAdapter listCardAdapter = new ListCardAdapter(getApplicationContext(), subscription, skuDetails);

    ListCardAdapter.setOnItemClickListener(this);

    //this will return RecyclerView: No adapter attached; skipping layout
    listView.setAdapter(listCardAdapter);

    removeProgressDialog();

}

I tried setting layoutManager inside onSkuDetails but it stops going to next line after setLayoutManager , I did some research and the results says:

  1. it's because of the setting of layoutManager.
  2. Must set the adapter onCreate() or main thread.

PS: I updated google play billing library to latest(4.0) that have changes on my classes related to this activity, google play billing is working.

I suggest you call the getPurchases() after the view is created in onStart of Activity or onViewCreated in Fragment. This is to avoid cases where response is returned before the view creation.

Next I am assuming your ListView.setAdapter(listCardAdapter); is a typo and you meant it to be listView . After setting the adapter, you should call listCardAdapter.notifyDataSetChanged() for the recyclerView to know the data has changed.

Best approach is to set the adapter without constructor params initially and later add the data via a method in the adapter which accepts subscription and sku details as the parameters.

Also another major thing to highlight, you will never get the item click because you are setting onClickListener on the adapter. You should pass an interface reference which gets called in onBindViewHolder method and onClickListener should be triggering callback on that listener reference.

Check this answer for the code snippet of the same. This is not part of question so it will be treated as a separate question.

https://stackoverflow.com/a/24471410/4491971

After some more research I found out that you just need to update adapter properties/data instead of setting the adapter again then just call notifyDataSetChanged()

private ListCardAdapter listCardAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    listView = findViewById(R.id.subscription_list);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);

    layoutManager.setOrientation(RecyclerView.VERTICAL);

    listView.setLayoutManager(layoutManager);

    listCardAdapter= new ListCardAdapter(getApplicationContext());

    listCardAdapter.setOnItemClickListener(this);

    listCardAdapter.setAdapter(subscriptionListCardAdapter);

    getPurchases();

}

@Override
public void onSkuDetails(List<SkuDetails> skuDetails) {

    removeProgressDialog();

    listCardAdapter.setData(subscription, skuDetails);

}

//ListCardAdapter 
public void setData(Subscription subscription, List<SkuDetails> skuDetailsList) {

    this.subscription = subscription;

    this.skuDetailsList = skuDetailsList;

    notifyDataSetChanged();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM