[英]Add objects to an arraylist from within inner class
我正在編寫一個應用程序,使用回調函數從我的在線數據庫中獲取數據到我的fragment
的RecyclerView
。 正如您將在下面的代碼中看到的那樣,我試圖顯示負責我最終結果的所有相關代碼片段。 如果您需要更多信息,請詢問我是否可以提供。
這是一個名為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
根本沒有返回任何內容。 我非常感謝你的幫助。
我想我同意你之前的評論,你的函數將總是返回一個空列表。 異步調用不起作用。 沒有回報價值。
不要在ListingModel中進行API調用。 首先獲取數據。 然后將其傳遞給ListingModel。 您可以使用update()方法接收獲取的項目列表,並強制刷新ListingModel。
在ItemAdapter上創建一個方法來設置列表並通過調用notifyDataSetChanged()通知適配器刷新; 如果獲取的列表不為空,則在成功響應中調用該方法。 即itemAdapter.setList(myArrayList)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.