Android 除非单击Edittext或将焦点放在加载上,否则不会显示RecyclerView
我尝试了这里提供的解决方案:但仍然没有显示,除非单击Edittext。数据是从特定URL的CSV文件调用的 主要活动代码:Android 除非单击Edittext或将焦点放在加载上,否则不会显示RecyclerView,android,xml,android-recyclerview,android-edittext,Android,Xml,Android Recyclerview,Android Edittext,我尝试了这里提供的解决方案:但仍然没有显示,除非单击Edittext。数据是从特定URL的CSV文件调用的 主要活动代码: //getting the recyclerview from xml recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new Lin
//getting the recyclerview from xml
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//getting CSV data from URL
DownloadFilesTask downloadFilesTask = new DownloadFilesTask();
downloadFilesTask.execute();
//initializing the productlist
productList = new ArrayList<>();
//creating recyclerview adapter
ProductAdapter adapter = new ProductAdapter(this, productList);
adapter.notifyItemInserted(productList.size());
adapter.notifyDataSetChanged();
//setting adapter to recyclerview
recyclerView.setAdapter(adapter);
//从xml获取recyclerview
recyclerView=(recyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
//从URL获取CSV数据
DownloadFilesTask DownloadFilesTask=新建DownloadFilesTask();
下载FileTask.execute();
//初始化产品列表
productList=新的ArrayList();
//创建recyclerview适配器
ProductAdapter=新的ProductAdapter(此,productList);
adapter.notifyItemInserted(productList.size());
adapter.notifyDataSetChanged();
//将适配器设置为recyclerview
recyclerView.setAdapter(适配器);
XML:
这里我只是结合我的代码。由于DownloadFilesTask是一个AsyncTask,因此您无法确保在此行之前添加产品的过程已经完成:
ProductAdapter adapter = new ProductAdapter(this, productList);
在调用AsyncTask.execute()之前,您可以在主线程上初始化productList和适配器;然后在异步任务完成后调用notifyDataSetChanged。你说得对!解决方案是放置recyclerView.setAdapter(适配器)循环后myprintCSVContent
函数中的code>。非常感谢。
private class DownloadFilesTask extends AsyncTask<URL, Void, List<String[]>> {
protected List<String[]> doInBackground(URL... urls) {
return downloadRemoteTextFileContent();
}
protected void onPostExecute(List<String[]> result) {
if(result != null){
printCVSContent(result);
}
}
}
private void printCVSContent(List<String[]> result){
String cvsColumn = "";
String distanceKM;
for (int i = 0; i < result.size(); i++){
String [] rows = result.get(i);
//cvsColumn += rows[0] + " " + rows[1] + " " + rows[2] + "\n";
//adding some items to our list
distanceKM = getDistance(latitude, longitude, Double.parseDouble(rows[2]),Double.parseDouble(rows[3]));
productList.add(
new Product(
rows[0],
rows[1].replace(';', ','),
rows[2],
rows[3],
rows[4],
rows[5],
rows[6],
distanceKM,
rows[7].replace(';', '\n')));
}
ProductAdapter adapter = new ProductAdapter(this, productList);