【发布时间】:2021-06-27 05:21:30
【问题描述】:
在片段的一侧,我正在使用ViewModel 将数据加载到RecyclerView。我的任务是提出我的网络请求的正确位置。现在我正在 ViewModel 类的 populateList 中执行此操作,但它似乎没有正确加载到视图中,所以我想知道是否应该在其他地方执行此操作?
视图模型
public class twoViewModel extends ViewModel {
MutableLiveData<ArrayList<User>> userLiveData;
ArrayList<User> userArrayList = new ArrayList<>();
public twoViewModel() {
userLiveData = new MutableLiveData<>();
// call your Rest API in init method
init();
}
public MutableLiveData<ArrayList<User>> getUserMutableLiveData() {
if(userLiveData==null)
userLiveData = new MutableLiveData<>();
return userLiveData;
}
public void init(){
populateList();
getUserMutableLiveData().setValue(userArrayList);
}
public void populateList() {
// Is this the right place?
System.out.println("********* populateList *********");
String url = "https://eliyah.com/wp-json/wp/v2/archive?page=1";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(com.android.volley.Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject jObj = response.getJSONObject(0);
// Get title
String title = jObj.getJSONObject("title").getString("rendered");
String[] desc = {"06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)"};
User user = new User();
try {
user.setTitle(title);
user.setDescription(desc[0]);
} catch (Exception e) {
e.printStackTrace();
}
userArrayList.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
// TODO figure out how to return userArrayList
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
MySingleton.getInstance(GlobleContextreRerence.instance.getApplicationContext()).addToRequestQueue(jsonArrayRequest);
编辑
在 ViewModel 中:
public void populateList() {
System.out.println("********* populateList *********");
WebRequest b = new WebRequest(userArrayList);
userArrayList = b.getx();
}
界面:
public interface VolleyCallBack {
ArrayList<User> onSuccess(ArrayList<User> result);
}
存储库:
public class WebRequest implements VolleyCallBack {
ArrayList<User> userArrayList;
public WebRequest(ArrayList<User> userArrayList) {
this.userArrayList = userArrayList;
}
@Override
public ArrayList<User> onSuccess(ArrayList<User> userArrayList) {
this.userArrayList = userArrayList;
return userArrayList;
}
public ArrayList<User> fromAPI(final VolleyCallBack callBack) {
String url = "https://eliyah.com/wp-json/wp/v2/archive?page=1";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(com.android.volley.Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject jObj = response.getJSONObject(0);
// Get title
String title = jObj.getJSONObject("title").getString("rendered");
String[] desc2 = {"06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)", "06/05/21 (03/24)"};
User user = new User();
try {
user.setTitle(title);
user.setDescription(desc2[0]);
} catch (Exception e) {
e.printStackTrace();
}
userArrayList.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
callBack.onSuccess(userArrayList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
MySingleton.getInstance(GlobleContextreRerence.instance.getApplicationContext()).addToRequestQueue(jsonArrayRequest);
return userArrayList;
}
public ArrayList<User> getData() {
ArrayList<User> arr;
arr = fromAPI(new VolleyCallBack() {
@Override
public ArrayList<User> onSuccess(ArrayList<User> result) {
return result;
}
});
return arr;
}
}
【问题讨论】:
标签: java android android-recyclerview android-viewmodel