这是我的情况。我有一个ListView片段。其中包含RSS下载的新闻标题。 当用户使用SwipeRefreshLayout刷新片段(从上下滑动手指作为推特应用)时,我检查是否存在新的RSS项目,并在下载新信息后我想刷新listView。
我使用AsyncTask下载新信息,并添加到我的avisosList(arraylist)但我的问题是在listView中。
如何使用新信息刷新ListView?
这是我的onCreateView方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.rss_avisos_fragment, container, false);
lv = (ListView) view.findViewById(R.id.rss_avisos_lv);
lv.setOnItemClickListener(this);
// Nuevo Elemento: Swipe Refresh Layout
swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.rss_avisos_swipeRefreshLayout);
swipeRefresh.setOnRefreshListener(this);
swipeRefresh.setColorScheme(android.R.color.holo_blue_dark,
android.R.color.holo_orange_light,
android.R.color.holo_green_dark,
android.R.color.holo_red_light);
return view;
}
onRefresh()
@Override
public void onRefresh() {
// TODO Auto-generated method stub
DowloadTest dow = new DowloadTest();
dow.execute();
}
DowloadesTest类扩展了AsynctTask。
public class DowloadTest extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
};
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
buscaNuevasNoticasRss();
return null;
}
private void buscaNuevasNoticasRss() {
// TODO Auto-generated method stub
//Here I check RSS and if is necessary I add new items into avisosList and execute a Toast.
...
if(avisosList.size()>=1){
String lang = (String) mirarIdioma();
insertarNuevosAvisos(avisosList, lang);
try {
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(getActivity().getBaseContext(), "Existen nuevos avisos.", Toast.LENGTH_LONG).show();
Looper.loop();
}
}.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}else{
try {
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(getActivity().getBaseContext(), "NO HAY NOTICIAS", Toast.LENGTH_LONG).show();
Looper.loop();
}
}.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
OnPostExecute。
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// Update your UI here
swipeRefresh.setRefreshing(false);
AvisosEnListaAdapter adapter = new AvisosEnListaAdapter(getActivity(), avisosList);
lv.setAdapter(adapter);
return;
}
我没有收到任何错误,但ListView没有刷新。
答案 0 :(得分:0)
我找到了很好的解决方案。
现在thi是onPostExecute代码:
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
// Update avisosList (arrayList) from DB
DataBaseHelper myDB = new DataBaseHelper(getActivity().getBaseContext());
myDB.openDataBase();
avisosList = myDB.getAllAvisos(lang);
tratarFechaParaPublicar(avisosList, lang);
adapter.refresh(avisosList);
swipeRefresh.setRefreshing(false);
return;
}
我需要在我的适配器中添加refresh(arrayList)
方法。
public void refresh(ArrayList<Aviso> itemsw) {
this.items = itemsw;
Log.d("Testing2", "AvisosEnListaAdapter. refresh(). Actualizando adaptador");
notifyDataSetChanged();
}