0
起初對不起我的英文不好。 我正在使用Android RecyclerView。從我的Activity向服務器發送數據請求,並通過調用onCreate方法中的sendRequestForHistory()方法從服務器獲得響應。在調用sendRequestForHistory()之後,初始化我的數據適配器,但在這裏我的ArrayList是空的。在sendRequestForHistory()方法中,從服務器獲取數據並添加到ArrayList。我的代碼是這樣的。Android RecyclerView適配器未初始化
public class HistoryActivity extends AppCompatActivity {
private Toolbar toolbar;
private ProgressDialog PD;
private RecyclerView recyclerView;
private HistoryAdapter historyAdapter;
private Boolean isInternetPresent = false;
private ConnectionDetector cd;
private TextView tvEmptyView;
private ArrayList<ItemHistory> itemHistories;
protected Handler handler;
private LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
dbHelper= new DatabaseHelper(this);
handler = new Handler();
itemHistories = new ArrayList<ItemHistory>();
toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setTitle(brandName);
setSupportActionBar(toolbar);
PD = new ProgressDialog(this);
PD.setMessage("Loading.....");
PD.setCancelable(false);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view_history);
recyclerView.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
sendRequestForHistory();
// Here itemHistories is empty.
historyAdapter = new HistoryAdapter(itemHistories, recyclerView);
recyclerView.setAdapter(historyAdapter);
historyAdapter.notifyDataSetChanged();
}
private void sendRequestForHistory() {
//send request to server and get response history data
// then history dada add to itemHistories ArrayList
itemHistories.add(new ItemHistory(requestReceiver[i], requestAmount[i],requestCost[i]);
}
}
可以顯示適配器代碼嗎? –
@馬修希勒,謝謝我認爲適配器班是好的。當我使用'historyAdapter = new HistoryAdapter(itemHistories,recyclerView); recyclerView.setAdapter(historyAdapter); historyAdapter.notifyDataSetChanged(); '在sendRequestForHistory()它工作正常。 – crazyCoder
sendRequestForHistory()以某種方式創建一個新線程?如果是這樣,那麼設置適配器需要等到這個完成。 –