0

Not to a string array. I'm downloading movie reviews via AsyncTask and storing the list in an array in a Recyclerview Adapter. There's a toast message in the returnReviewData method in case the data is unavailable. I'd like to display a text message instead.

I know how to convert an ArrayList to a String using a String builder. However, I haven't been able to do it here(incl. like they do in the threads below). Is it possible to do it inside the if statement? Thank you in advance.

Best way to convert an ArrayList to a string

how to convert arraylist into string?

public class DetailActivity extends AppCompatActivity implements MovieTrailerAdapter.MovieTrailerAdapterOnClickHandler, AsyncTaskReviewInterface {

//Tag for the log messages
    private static final String TAG = DetailActivity.class.getSimpleName();

    private ArrayList<MovieReview> simpleJsonMovieReviewData = new ArrayList<>();
 private Context context;
    private RecyclerView mRecyclerViewReview;
 private MovieReviewAdapter movieReviewAdapter;
 Movie movie;

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

   context = getApplicationContext();
        mRecyclerViewReview = (RecyclerView) findViewById(R.id.recyclerview_review);
   movieReviewAdapter = new MovieReviewAdapter(simpleJsonMovieReviewData, context);
      mRecyclerViewReview.setAdapter(movieReviewAdapter);


        RecyclerView.LayoutManager mReviewLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerViewReview.setLayoutManager(mReviewLayoutManager);

 if (getIntent() != null && getIntent().getExtras() != null)
        {
            movie = getIntent().getExtras().getParcelable("Movie");
   movieId = movie.getMovieId();

            MovieReviewAsyncTask myReviewTask = new MovieReviewAsyncTask(this);
            myReviewTask.execute(movieId);
}

public void returnReviewData(ArrayList<MovieReview> simpleJsonMovieReviewData)
    {
        movieReviewAdapter = new MovieReviewAdapter(simpleJsonMovieReviewData, DetailActivity.this);
        mRecyclerViewReview.setAdapter(movieReviewAdapter);

        if (simpleJsonMovieReviewData.size() == 0)
        {
            Toast.makeText(DetailActivity.this, R.string.review_unavailable, Toast.LENGTH_SHORT).show();
        }
    }
}
LeadBox4
  • 83
  • 2
  • 11
  • I don't get what you want to achieve. Converting an ArrayList to a string should work inside a RecyclerViewAdapter exactly like everywhere else in your code. Where does it fail? – Alexander Hoffmann Jan 18 '19 at 15:08
  • @AlexanderHoffmann simpleJsonMovieReviewData can't be converted; setText doesn't work.I just want to display a message "Data currently unavailable" which will be displayed on screen. – LeadBox4 Jan 18 '19 at 15:12
  • 1
    @LeadBox4 check [this](https://stackoverflow.com/a/27801394/3831557) – Rajen Raiyarela Jan 18 '19 at 15:18
  • Ok, if I understand it correctly, you want to have some message in your UI, when no data is available. Then your current approach isn't ideal. Your RecyclerView expects a list of `MovieReview` objects. You can't just put a string in it. Instead, check out what your activity should do if your request doesn't return data here: https://stackoverflow.com/questions/28217436/how-to-show-an-empty-view-with-a-recyclerview – Alexander Hoffmann Jan 18 '19 at 15:18
  • @AlexanderHoffmann So, I would set the TextView to invisible and then set it visible in the if statement? Just replace the toast message? – LeadBox4 Jan 18 '19 at 15:22
  • 1
    @LeadBox4 Almost. Don't use `invisible` here. Set the visibility to `gone` per default and make your RecyclerView `visible` if you get movie data. If you don't get data, set the visibility of your RecyclerView to `gone` and make your TextView `visible` in your `if` statement. – Alexander Hoffmann Jan 18 '19 at 15:27
  • @RajenRaiyarelaThank you for the link. So, it's better to put the code in the adapter class? Then, I don't need to create a textview in xml? – LeadBox4 Jan 18 '19 at 15:36
  • @AlexanderHoffmannDo I put the emptylist TextView in the activity xml or the list xml? – LeadBox4 Jan 18 '19 at 16:27
  • @AlexanderHoffmannSolved! I did it like in the most accepted answer in the link you posted. Can you put that in an answer here so that I can give you points? Thank you. – LeadBox4 Jan 18 '19 at 17:35
  • @RajenRaiyarela Thank you for the links. – LeadBox4 Jan 18 '19 at 17:36

0 Answers0