我在Android上遇到ListView
問題。其實不是ListView
,因爲它是在Sherlock Fragment
上實現的。所以特別是: 我有2個班,一個是Fragment
,一個是正常的Activity
。 Activity
從網上下載一個文件,在Fragment
中有一個包含下載文件的列表。
這是我對Fragment
適配器:自定義ListView不刷新
File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Sample Folder");
int i = 0;
while (i < mydownload.list().length) {
adapter.add(mydownload.list()[i]);
i++;
}
setListAdapter(adapter);
所以適配器捕獲所有的文件示例文件夾,並把它在名單上。當我下載一個文件時,適配器不刷新列表(我認爲它是在不同的上下文中,但我不知道解決方案)。刷新我必須關閉並打開應用程序。我嘗試了很多可能的解決方案,但沒有任何幫助。
編輯:我Fragment
代碼:
public class AppleFragment extends SherlockListFragment{
/** An array of items to display in ArrayList */
private static ArrayAdapter<String> adapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
/** Creating array adapter to set data in listview */
adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), R.layout.liststyle, new ArrayList<String>());
View v = super.onCreateView(inflater, container, savedInstanceState);
/** Setting the array adapter to the listview */
File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale");
int i = 0;
while (i < mydownload.list().length) {
adapter.add(mydownload.list()[i]);
i++;
}
setListAdapter(adapter);
return v;
}
@Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id){
String gazza = (String) adapter.getItemAtPosition(position);
File pdf = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale" + "/" + gazza);
try {
Uri path = Uri.fromFile(pdf);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
/** Setting the multiselect choice mode for the listview */
//getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick (AdapterView<?> arg0, View view, int position, long id){
final int pos = position;
String gazza = (String) arg0.getItemAtPosition(position);
final File pdf = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale" + "/" + gazza);
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
pdf.delete();
adapter.remove(adapter.getItem(pos));
adapter.notifyDataSetChanged();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
dialog.dismiss();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Vuoi eliminare la Gazzetta?").setPositiveButton("Elimina", dialogClickListener)
.setNegativeButton("Annulla", dialogClickListener).show();
return true;
}
});
}
}
下載活動:在你的適配器
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();
}
}
您是否嘗試在獲取新數據後調用適配器上的notifyDataSetChanged()方法? –
我在活動中獲取新數據,如何將此metod命名爲片段上的適配器?對不起,但是我的第一個應用程序! – bott91
好吧我對碎片不是很熟悉,但我會盡力幫助你。我想你正在調用一個函數來更新你的活動列表視圖,對吧?在這種情況下,您仍然可以在更新函數末尾調用我建議您使用的方法,如下所示:adapter.notifyDataSetChanged()。 –