我需要从回调方法

时间:2018-06-03 07:43:32

标签: android firebase asynchronous callback futuretask

我需要从以下代码返回ListView。 getUser()是一个从数据库返回用户的函数,它以异步方式执行。顺便说一句,我使用的是Firebase。我需要它在回调后从数据库中检索数据返回ListView,它是onCallback()中的代码,但是我不能在其中放置一个返回,它必须在它之外。但是,当然,如果我把它放在外面,它首先执行而不是检索数据,因为它是异步的。

@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()
}

我试图寻找如何解决它,我发现也许它可以通过FutureTask方法完成,但之前从未使用它,我也无法成功实现它。你能帮我怎么做吗?也许您知道如何正确实施它,或者您可能知道另一种解决方案......提前感谢您。

0 个答案:

没有答案