gpt4 book ai didi

android - RecyclerView 不会立即显示任何内容

转载 作者:行者123 更新时间:2023-11-30 00:29:13 26 4
gpt4 key购买 nike

我正在尝试解决与 RecyclerView 相关的问题。当我第一次尝试显示对象列表时,我什么也没看到。由于已知是异步的 volley 请求,数据得以传递。因此,我尝试了各种解决方案,例如 onDataSetChanged() 但没有任何反应。我想指出该解决方案有效,第二次查看列表时我可以毫无问题地看到它。

这是我的代码:

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;

private static MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);

recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
adapter = new MyAdapter(this,initData());
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}

private List<ParentObject> initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();

for(TitleParent title:titles)
{
List<Object> childList = getList(title);
title.setChildObjectList(childList);
parentObject.add(title);
}

return parentObject;
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}

@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}

private List<Object> getList(final TitleParent title){
final List<Object> childList = new ArrayList<>();

// Tag used to cancel the request
String tag_string_req = "request";

StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_DASHBOARD, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d("POL", "Response: " + response.toString());

try {
JSONObject jObj = new JSONObject(response);

for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
String certificate = user.getString("numero_contratto");
if(title.getTitle().contains(certificate)){
String scadenza = user.getString("scadenza");
String totale = user.getString("totale");
childList.add(new TitleChild(scadenza,totale));
}
}
Log.d("TRY", "Response: " + response.toString());
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
// Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("DASH", "Login Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(),
// error.getMessage(), Toast.LENGTH_LONG).show();
}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

return childList;
}

感谢您的帮助!

最佳答案

有几点

  1. get list 方法在发出 volley 请求后返回空列表。
  2. 确保列出来自服务器的填充数据,然后调用设置的数据。

你必须尝试像下面这样的东西。

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;

private static MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);

recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
// show some loading bar
initData();

}

private void initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();

for(TitleParent title:titles)
{
List<Object> childList = getList(title);
title.setChildObjectList(childList);
parentObject.add(title);
}


}
private void setData(List<ParentObject> parentObject) {
// load the data here
adapter = new MyAdapter(this,parentObject);
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}


@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}

@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}

private List<Object> getList(final TitleParent title){
final List<Object> childList = new ArrayList<>();

// Tag used to cancel the request
String tag_string_req = "request";

StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_DASHBOARD, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d("POL", "Response: " + response.toString());

try {
JSONObject jObj = new JSONObject(response);

for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
String certificate = user.getString("numero_contratto");
if(title.getTitle().contains(certificate)){
String scadenza = user.getString("scadenza");
String totale = user.getString("totale");
childList.add(new TitleChild(scadenza,totale));
}
setData(childList);
}
Log.d("TRY", "Response: " + response.toString());

} catch (JSONException e) {
// JSON error
e.printStackTrace();
// Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("DASH", "Login Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(),
// error.getMessage(), Toast.LENGTH_LONG).show();
}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

return childList;
}

关于android - RecyclerView 不会立即显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44763392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com