Android 自定义ListView没有';t刷新
我对android上的Android 自定义ListView没有';t刷新,android,listview,refresh,android-fragmentactivity,Android,Listview,Refresh,Android Fragmentactivity,我对android上的列表视图有问题。不是真正的列表视图,因为它是在Sherlock片段上实现的。因此,特别是: 我有两个类,一个是片段,一个是正常的活动。活动从web下载一个文件,在片段中有一个包含已下载文件的列表。 这是我的片段的适配器: File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Sample Folder"); int i = 0; while ( i < mydownload.l
列表视图有问题。不是真正的列表视图
,因为它是在Sherlock片段
上实现的。因此,特别是:
我有两个类,一个是片段
,一个是正常的活动
。活动
从web下载一个文件,在片段
中有一个包含已下载文件的列表。
这是我的片段的适配器
:
File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Sample Folder");
int i = 0;
while ( i < mydownload.list().length) {
adapter.add(mydownload.list()[i]);
i++;
}
setListAdapter(adapter);
下载活动:
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale");
if (!mydownload.exists()){
mydownload.mkdir();
}
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
String url = sUrl[0];
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Gazzetta " + sUrl[1])
.setDescription(sUrl [2] + ". In download..")
.setDestinationInExternalPublicDir("/Gazzetta Ufficiale", sUrl[1] + ".pdf");
manager.enqueue(request);
}
catch (Exception e) {
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
toast = Toast.makeText(Gazzetta_detail.this, "Download in corso...", Toast.LENGTH_SHORT);
toast.show();
}
}
私有类下载文件扩展异步任务{
@凌驾
受保护的字符串背景(字符串…sUrl){
试一试{
File mydownload=新文件(Environment.getExternalStorageDirectory()+“/Gazzetta Ufficiale”);
如果(!mydownload.exists()){
mydownload.mkdir();
}
DownloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD\u服务);
字符串url=sUrl[0];
DownloadManager.Request=newdownloadmanager.Request(Uri.parse(url));
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.HONEYCOMB){
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.request.VISIBILITY\u VISIBLE\u NOTIFY\u完成);
}
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK|WIFI | DownloadManager.Request.NETWORK|u MOBILE)
.setAllowedOverRoaming(假)
.setTitle(“Gazetta”+sUrl[1])
.setDescription(sUrl[2]+“.In下载..”)
.setdestinationnexternalpublicdir(“/Gazzetta Ufficiale”,sUrl[1]+”.pdf”);
排队(请求);
}
捕获(例外e){
}
返回null;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
toast=toast.makeText(Gazzetta_detail.this,“下载到corso…”,toast.LENGTH_SHORT);
toast.show();
}
}
在适配器上调用notifyDataSetChanged()
有关如何/何时调用notifyDataSetChanged()的其他详细信息,请参见本文档
.获取新数据后,是否尝试调用适配器上的notifyDataSetChanged()方法?如果在“活动”中获取新数据,如何为片段上的适配器调用此方法?对不起,这是我的第一份申请!好的,我对碎片不是很熟悉,但我会尽力帮助你。我想您正在调用一个函数来更新活动中的listview,对吗?在这种情况下,您仍然可以调用我在update函数末尾建议您使用的方法,在其内部类似于:adapter.notifyDataSetChanged()。我编辑问题,查看我的整个代码中的2个类我有点困惑。。您将AsyncTask称为活动,这实际上是错误的。我仍然认为您仅在应用程序启动时使用onCreateView()触发列表更新。为了简化操作,只需在布局中添加一个按钮,该按钮只需调用adapter.notifyDataSetChanged(),然后查看是否发生了什么情况,如果是,这意味着在获取数据后,您没有正确触发适配器上的更新,或者您没有使用新数据更新列表。
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale");
if (!mydownload.exists()){
mydownload.mkdir();
}
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
String url = sUrl[0];
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Gazzetta " + sUrl[1])
.setDescription(sUrl [2] + ". In download..")
.setDestinationInExternalPublicDir("/Gazzetta Ufficiale", sUrl[1] + ".pdf");
manager.enqueue(request);
}
catch (Exception e) {
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
toast = Toast.makeText(Gazzetta_detail.this, "Download in corso...", Toast.LENGTH_SHORT);
toast.show();
}
}