0

I need to return a ListView from the following code. getUser() is a function to return a user from the database, that does it asynchronously. I'm using Firebase, by the way. I need it to return the ListView after the Callback from retrieving data from the database, it is the code in onCallback(), but I can't put a return within that, it has to be outside it. But of course, if I put outside it, it executes first than the retrieving data, since it is asynchronous.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    final View listViewItem = inflater.inflate(R.layout.translation_item_layout, null, true);

    final TextView textViewUsername = (TextView) listViewItem.findViewById(R.id.textViewUsername);
    final TextView textViewBio = (TextView) listViewItem.findViewById(R.id.textViewBio);
    final TextView textViewTranslationText = (TextView) listViewItem.findViewById(R.id.textViewTranslationText);
    final TextView textViewSource = (TextView) listViewItem.findViewById((R.id.textViewSource));
    final TextView textViewScore = (TextView) listViewItem.findViewById((R.id.textViewScore));
    final TextView textViewCommentsCount = (TextView) listViewItem.findViewById((R.id.textViewCommentsCount));
    final CircleImageView imageViewProfilePicture = (CircleImageView) listViewItem.findViewById(R.id.imageProfile);

    translation = translations.get(position);

    getUser(new FirebaseCallback<User>() {
                @Override
                public void onCallback(User data) {
                    textViewUsername.setText(user.getNickname());
                    textViewBio.setText(user.getBio());
                    Glide.with(getContext()).load(user.getImage()).into(imageViewProfilePicture);

                    textViewTranslationText.setText(translation.getText());
                    textViewSource.setText(translation.getSource());
                    textViewScore.setText(String.valueOf(translation.getLikes() - translation.getDislikes()));
                    textViewCommentsCount.setText(String.valueOf(translation.getComments()));
                    //return listViewItem -> I need to do the effect of returning here, after the callback
                }
            });
    return listViewItem; //If i leave it here, it executes asynchronously, first than the code in the onCallBack()
}

I tried to look for how to solve it, and I found maybe it can be done through the method FutureTask, but never used it before and I couldn't implement it successfully. Can you help me how to do that? Perhaps you know how to implement it properly or maybe there is another solution you know... Thank you in advance.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • I recommend you see the last part of my anwser from this **[post](https://stackoverflow.com/questions/48499310/firestore-object-with-inner-object/48500679)** and also take a look at this **[video](https://www.youtube.com/watch?v=0ofkvm97i0s)**. – Alex Mamo Jun 04 '18 at 16:32
  • @AlexMamo Thank you! I already have watched one video yours as a reference. But Im not sure how to apply to my code. Im still learning Android programming. This class is to show the items in the list view. The only methods are just the constructor and this Override of getView... I'm not sure where to place the method call. Can you help me. please? – Hyung Tae Carapeto Figur Jun 05 '18 at 16:03

0 Answers0