0

I am creating an application which fetches movies list from Tmdb Api. My code is for main Activity is

public class MainActivityFragment extends Fragment { 
  public class FetchImageTask extends AsyncTask<...>{ 
    //
  }
}

And I have another adapter class

public class MovieAdapter extends ArrayAdapter<MovieImage> {

private static int page_nmber = 1;

public MovieAdapter(Context context, List<MovieImage> objects) {
    super(context, 0 , objects);
}

final String LOG_TAG = MovieAdapter.class.getSimpleName();

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(getCount() == position + 5 ){
        //Log.v(LOG_TAG, " last two images");
    }

    MovieImage movieImage = getItem(position);

    View rootView = LayoutInflater.from(getContext())
                    .inflate(R.layout.list_item_movie, parent, false);

    ImageView imageView = (ImageView) rootView.findViewById(R.id.list_item_movie_image);
    Picasso.with(getContext()).load(movieImage.getImage_path())
            .placeholder(R.drawable.loading)
            .fit()
            .into(imageView);

    return rootView;
}

public void fetchExtraImages(){
    MainActivityFragment.FetchImageTask fetchImageTask;
    fetchImageTask = new MainActivityFragment.FetchImageTask();
    //Gives me error saying this is not an enclosing class
}

Actually I want to create another object for FetchImageTask object and call next page(page 2) results and append result to respective adapter.

Any help is appreciated. :)

diogojme
  • 2,309
  • 1
  • 19
  • 25
  • probably like `MainActivityFragment objMainActivityFragment =new MainActivityFragment (); fetchImageTask =objMainActivityFragment.new FetchImageTask()` – ρяσѕρєя K Dec 21 '15 at 17:01
  • 1
    `AsyncTask` does not have to be an inner class to work. You can use callbacks to have `AsyncTask` in a separate class file of its own, but to post its results anywhere. – NoChinDeluxe Dec 21 '15 at 17:01
  • 1
    See this post [here](http://stackoverflow.com/questions/4070716/instantiating-inner-class) that already answers your question, if you really want your AsyncTask to be a nested class, consider declaring it as static. – alxscms Dec 21 '15 at 17:02
  • I think you need an interface to communicate between the activity and your fragment(s). – Kristy Welsh Dec 21 '15 at 17:15

1 Answers1

2

I recommend you to not use an async task inside your activity.

1 - You could have to use the same task by another class,activity or fragment

2 - You activity will have single responsibility (SOLID)

3 - You can use callbacks to communicate to activity, fragment or any class.

Create an interface separated:

public interface FetchImageCallback{
   void onImageFetched(OBJECT);//replace by your 
}

Make your activity implements FetchImageCallback so you can get the images, ex:

public class MainActivityFragment extends Fragment implements FetchImageCallback{

 public void fetchExtraImages(){
   FetchImageTask fetchImageTask = new FetchImageTask(this); //this is your callback
 }

 void onImageFetched(OBJECT){
     //do what you want to do with the images fetched (OBJECT)
 }
}

If you really want to use as inner class (not recommended) just declare FetchImageTask as static class:

public static class FetchImageTask{
  //some code
}
diogojme
  • 2,309
  • 1
  • 19
  • 25
  • Thanks. I have removed my Fetch Image Task as InnerClass and placed it as a saperate class. The result is passed via interface callback.(ie., listenerClass inside MainActivityFragment). It works fine. I have requested tmdb api to fetch movies lists.It returned just a single page ( having 20 movie entries ). I want to call FetchImageTask().execute() method in getView() method( overriden in custom Array Adapter ) for page 2. I 'm having problem with that – ramana reddy Dec 21 '15 at 18:43
  • Finally I got it. I need to pass my MainActivityFragment innerclass( ie Interface listner ) to Adapter. .........Thank you guys – ramana reddy Dec 21 '15 at 19:14