0

I'm using AsyncTaskLoader in my project without issues in activities but now I want to trigger it inside the adapter. The list item populated by RecyclerView has multiple elements which do certain actions via API and I need to implement AsyncTaskLoader for those different items. How can I use it inside the adapter?

I tried to add this code as a start

public class NewsCommentsAdapter extends RecyclerView.Adapter<NewsCommentsAdapter.MyViewHolder> implements LoaderManager.LoaderCallbacks<List<PostResponse>>

And inside onBindViewHolder

holder.thumbUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ConnectionDetector mConnectionDetector;
            mConnectionDetector = new ConnectionDetector(mContext);
            if (mConnectionDetector.isConnectingToInternet() == false) {

            } else {
                android.app.LoaderManager loaderManager = getLoaderManager();
                loaderManager.initLoader(LOADER_ID, null, this);
            }
        }
    });

But I get an error "cannot resolve method getLoaderManager" so I assume there's another way to implement AsyncTaskLoader in the adapter.

PHP User
  • 2,350
  • 6
  • 46
  • 87
  • Why are you using LoaderManager in recyclerview adapter? – devmike01 May 15 '19 at 11:58
  • to send post request when the user tabs the thumb up button, thumb down or add comment button so it's not a list item tab but certain views in the list item can I access them in the Activity? The activity loads data when opened via AsyncTaskLoader too – PHP User May 15 '19 at 11:59
  • Use a callback to listen to item click, then do whatever you want inside the callback method in your Activity. – devmike01 May 15 '19 at 12:24
  • if you mean something liks this: cpmmentsRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, cpmmentsRecyclerView, new RecyclerTouchListener.ClickListener() { - I get the same value for all views in the recyclerview like "2131296363" – PHP User May 15 '19 at 12:34
  • I'm still learning so hope you help me as you can – PHP User May 15 '19 at 12:35
  • I got you now I created a method in the activity which listens to the view click in the adapter holder.thumbUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SingleNewsActivity.commentLikeListener(comment.getCommentId()); } }); is that what you mean? – PHP User May 15 '19 at 12:42
  • this method works but can't init getLoaderManager from that method because it's static – PHP User May 15 '19 at 12:47

0 Answers0