从内部类中将对象添加到arraylist

时间:2019-03-23 02:57:40

标签: java android arraylist callback

我正在编写一个应用程序,该应用程序使用回调函数将在线数据库中的数据提取到RecyclerView中的fragment中。正如您将在下面的代码中看到的那样,我试图显示所有与最终结果有关的相关代码。如果您需要更多信息,请提出要求,以便我提供。

这是一个称为 Question.java

的类
public class Question {
    public Integer postid;
    public String title;
    public String content;
    public String tags;
    public String created;

}

然后是回调类: CallbackQuestions.java

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.example.myapp;

public class CallbackQuestions implements Serializable{
    public int total = -1;
    public List<Question> data = new ArrayList<>();
}

我的ItemModel类: ItemModel.java

public class ItemModel {
    private String Title;
    private String Description;
    private String Tags;
    private String Created;

    public ItemModel(String Title, String Description, String Tags, String Created) {
        this.Title = Title;
        this.Description = Description;
        this.Tags = Tags;
        this.Created = Created;
    }

    public String getCreated() {
        return Created;
    }

    public void setCreated(String Created) {
        this.Created = Created;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String Title) {
        this.Title = Title;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String Description) {
        this.Description = Description;
    }

    public String getTags() {
        return Tags;
    }

    public void setTags(String Tags) {
        this.Tags = Tags;
    }
}

最后,这就是让我头疼的问题,它使它无法正常工作 ListingModel.java

@Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ListingModel listingModel = mArrayList.get(position);

        ((NormalViewHolder) holder).tv_card_header.setText(mArrayList.get(position).getHeader());

        ((NormalViewHolder) holder).listing_recycler_view.setHasFixedSize(true);
        RecyclerView.LayoutManager normalLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        ((NormalViewHolder) holder).listing_recycler_view.setLayoutManager(normalLayoutManager);

        ArrayList<ItemModel> list = loadQuestionsList();

        ItemAdapter itemAdapter = new ItemAdapter(list);
        ((NormalViewHolder) holder).listing_recycler_view.setAdapter(itemAdapter);

        Log.d("MyAdapter", "position: " + position);
    }

    public ArrayList<ItemModel> loadQuestionsList() {
        final ArrayList<ItemModel> mArrayList = new ArrayList<>();
        int start = 0;
        String sort = "recent";
        API api = CallJson.callJson();
        Call<CallbackPosts> callbackQuestionsCall = api.QuestionsAll(BaseUrlConfig.RequestLoadMore, start, sort);
        callbackQuestionsCall.enqueue(new Callback<CallbackQuestions>() {
            @Override
            public void onResponse(Call<CallbackQuestions> call, Response<CallbackQuestions> response) {
                CallbackQuestions callbackQuestions = response.body();
                for (QuestionsAll thisQuestion : callbackQuestions.data){
                    mArrayList.add(new ItemModel(thisQuestion.Title, thisQuestion.Description, thisQuestion.Tags, thisQuestion.Created));
                }
                 myArrayList = mArrayList;
            }

            @Override
            public void onFailure(Call<CallbackQuestions> call, Throwable t) {
                if (!call.isCanceled());
            }
        });
        return myArrayList;
    }

问题是我的ArrayLists根本没有返回任何内容,尽管已按照日志成功运行了回调。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我想我同意您之前的评论,您的函数将始终返回一个空列表。异步调用不能那样工作。没有返回值。

不要在ListingModel内进行API调用。首先获取数据。然后将其传递到ListingModel中。您可以使用update()方法来接收提取的项目列表,并强制刷新ListingModel。

在ItemAdapter上创建一个方法,该方法设置列表并通过调用notifyDataSetChanged()通知适配器刷新。如果获取的列表不为空,则在成功响应中调用该方法。即itemAdapter.setList(myArrayList)。