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.