简体   繁体   English

从内部类中添加对象到arraylist

[英]Add objects to an arraylist from within inner class

I am writing an app to fetch data from my online database using a callback function to a RecyclerView in my fragment . 我正在编写一个应用程序,使用回调函数从我的在线数据库中获取数据到我的fragmentRecyclerView As you will see in my code below I have tried to show all the relevant pieces of code that are responsible for my final result down there. 正如您将在下面的代码中看到的那样,我试图显示负责我最终结果的所有相关代码片段。 Should you require more information please ask so that I may provide it. 如果您需要更多信息,请询问我是否可以提供。

Here is one class called Question.java 这是一个名为Question.java的

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

}

then the callback class: CallbackQuestions.java 然后是回调类: 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<>();
}

My ItemModel class: ItemModel.java 我的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;
    }
}

Then lastly here is what is giving me headache to get it working ListingModel.java 然后最后这里让我很头疼让它工作的是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;
    }

The problem is that my ArrayLists is not returning anything at all despite the callback running successfully as per logs. 问题是尽管按日志成功运行了回调,但我的ArrayLists根本没有返回任何内容。 I would appreciate your help so much. 我非常感谢你的帮助。

I think i agree with the comment you got on earlier, your function will always return an empty list. 我想我同意你之前的评论,你的函数将总是返回一个空列表。 Asynchronous calls don't work that way. 异步调用不起作用。 There is no return value. 没有回报价值。

Don't make API calls inside the ListingModel. 不要在ListingModel中进行API调用。 Fetch the data first. 首先获取数据。 Then pass it into the ListingModel. 然后将其传递给ListingModel。 You can have an update() method that receives the list of items fetched and forces a refresh of the ListingModel. 您可以使用update()方法接收获取的项目列表,并强制刷新ListingModel。

On ItemAdapter create a method that set the list and notify the adapter to refresh by calling notifyDataSetChanged(); 在ItemAdapter上创建一个方法来设置列表并通过调用notifyDataSetChanged()通知适配器刷新; Then call that method inside the success response if the list fetched is not empty. 如果获取的列表不为空,则在成功响应中调用该方法。 ie itemAdapter.setList(myArrayList). 即itemAdapter.setList(myArrayList)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM