-3

My problem is that I get the data from Retrofit and save all the array So, when I access the array to flip the date, I get an error in setText because it doesn't accept LocalData

I looked at this question, but because I work with output and an array, it didn't help

How to format LocalDate to string?

Getting the main:

public void onResponse(Call<Example> call, Response<Example> response) {
    Example examples = response.body();
     for(int i = 0; i < examples.getResponse().size(); i++){
        String f_name = response.body().getResponse().get(i).getfName();
        String l_name = response.body().getResponse().get(i).getlName();
        String SpecName = response.body().getResponse().get(i).getSpecialty()
                                         .iterator().next().getName();
        String SpecId =  response.body().getResponse().iterator().next().getSpecialty()
                                         .iterator().next().getSpecialtyId().toString();
        String AvatarUrl = response.body().getResponse().get(i).getAvatrUrl();
        DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.getDefault());
        LocalDate date = LocalDate.parse(response.body().getResponse().get(i).getBirthday(), format);

        workerList.add(new Worker(f_name, l_name, date, SpecName, SpecId, AvatarUrl));

     }
    setPersonRecycler(workerList);
}

Now the Holder itself:

public void onBindViewHolder(@NonNull PersonViewHolder holder, int position) {
    holder.lname.setText(workerList.get(position).getLname());
    holder.fname.setText(workerList.get(position).getFname());
    holder.birthday.setText(workerList.get(position).getBirthday());
    Picasso.with(context).load(workerList.get(position).getAvatarUrl()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(holder.imageView);
        holder.SpecName.setText(workerList.get(position).getSpecName());
        holder.id.setText(workerList.get(position).getSpecId());
    }

The error is this:

Cannot resolve method 'setText(java.time.LocalDate)'
Liannes
  • 7
  • 5
  • 1
    `setText` works with `String`s. You have to convert your date to a String first – QBrute Jul 20 '21 at 12:14
  • Does this answer your question? [How to format LocalDate to string?](https://stackoverflow.com/questions/28177370/how-to-format-localdate-to-string) – Federico klez Culloca Jul 20 '21 at 12:15
  • @FedericoklezCulloca, no – Liannes Jul 20 '21 at 12:16
  • @Liannes it should, since it shows how to convert a date to string. – Federico klez Culloca Jul 20 '21 at 12:17
  • @FedericoklezCulloca, I already tried that, but it didn't work – Liannes Jul 20 '21 at 12:18
  • 2
    Then please [edit] your question to show what you tried and how that went wrong. Otherwise people will keep suggesting things you may have already tried. – Federico klez Culloca Jul 20 '21 at 12:19
  • 1
    What does "Didn't work" mean? You text field takes `String`, not `LocalDate`. So you just need to format it as string, as described in the linked question. – QBrute Jul 20 '21 at 12:21
  • @QBrute, I work with an array, not one line – Liannes Jul 20 '21 at 12:25
  • @Liannes yes, but `workerList.get(position).getBirthday()` is still a `LocalDate`, it doesn't matter that it comes from an array (a `List`, actually). `workerList.get(position).getBirthday()` needs to be converted to a string, in the way detailed in the linked question. There's not much way around it unless you're willing to change Android's classes to make components accept dates. And even then, you'll have to convert them internally anyway. – Federico klez Culloca Jul 20 '21 at 12:27
  • @Liannes The error states `Cannot resolve method 'setText(java.time.LocalDate)'`, which means that the line `holder.birthday.setText(workerList.get(position).getBirthday());` doesn't work like this. You need to convert the result of `workerList.get(position).getBirthday()`, which is a `LocalDate` to a `String` first, then put it into your text field. This is totally independent of what data structure you're using – QBrute Jul 20 '21 at 12:28

1 Answers1

0

As we've mentioned in the comments, you need to convert the result of workerList.get(position).getBirthday(), which is a LocalDate to String, before putting it into your text box.

This question shows how to do that in general.

In your case something like this should work:

LocalDate birthdayDate = workerList.get(position).getBirthday();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String formattedBirthday = birthdayDate.format(formatter);
holder.birthday.setText(formattedBirthday);
QBrute
  • 4,405
  • 6
  • 34
  • 40