我有这个AsyncTask,当isOnline()从HTTPost请求的数据加载listview时...如果它不在线,那么它从本地SQLite数据库加载..
我的AsyncTask获取数据(在线 - >来自在线数据库,离线 - >来自我的SQLite数据库)
class ObtenirAmics extends AsyncTask< String, ArrayList<Amic> , ArrayList<Amic> >{
String data = null;
protected void onPreExecute() {
refresh_amics.setVisibility(View.VISIBLE);
separator_refresh_amics.setVisibility(View.VISIBLE);
progress_amics.setVisibility(View.VISIBLE);
separator_amics.setVisibility(View.GONE);
tv_amics.setVisibility(View.GONE);
tv_amics.setText("FRIENDS");
scrollview_amics.scrollTo(0, 0);
BD=new AmicsSQLiteHelper(getActivity());
BD.open();
if(isOnline()){
BD.removeTotsAmics(); // If is Online, remove all the friends from
SQLite Database..
and later it add all the friends another time.
}
}
protected ArrayList<Amic> doInBackground(String... params) {
ArrayList<Amic> amics =new ArrayList<Amic>();
if(isOnline()){
String data;
SharedPreferences dades_login = getActivity().getSharedPreferences("perfil",
getActivity().MODE_PRIVATE);
String id = dades_login.getString("id", "");
List<NameValuePair> parametres = new ArrayList<NameValuePair>();
parametres.add(new BasicNameValuePair("id", id));
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url_obtenir_amics);
request.setEntity(new UrlEncodedFormEntity(parametres));
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
data = EntityUtils.toString(entity);
Log.e("AMICS OBTINGUTS", data);
try {
JSONArray json = new JSONArray(data);
for (int i = 0; i < json.length(); i++) {
JSONObject obj = json.getJSONObject(i);
String nom = obj.getString("nombre");
Log.e("PERSONA TROBADA:", nom);
addAndSet_Amics(nom); // Its a function that calls a runnable
to add to arraylist and setadapter
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
}
else{
amics = BD.getAllElements(); // HERE IS THE FUNCION THAT GETS
ALL THE DATA FROM SQLite
adapter.notifyDataSetChanged();
}
return amics;
}
protected void onPostExecute(ArrayList<Amic> amics) {
refresh_amics.setVisibility(View.GONE);
separator_refresh_amics.setVisibility(View.GONE);
progress_amics.setVisibility(View.GONE);
if(amics.size()>0){
tv_amics.setVisibility(View.VISIBLE);
separator_amics.setVisibility(View.VISIBLE);
}
listView_Amics.setAdapter(adapter);
Utility.setListViewHeightBasedOnChildren(listView_Amics,scrollview_amics);
}
}
在我的Helper SQLite类中,我有一个函数返回包含我数据库所有行的列表..(logcat显示所有朋友,但在listview中没有显示任何内容)
public ArrayList<Amic> GetAllValues()
{
ArrayList<Amic> list = new ArrayList<Amic>();
Cursor cursor = myDataBase.query(DATABASE_TABLE, cols, null, null, null, null, null);
if (cursor.moveToFirst())
{
do
{
list.add(new Amic(cursor.getString(0)));
Log.e("Afegint",cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
答案 0 :(得分:0)
在数据切换时,您还必须更新适配器数据。
并在更新后,您需要调用adapter.notifyDataSetChanged();.
以下是一个例子:
在适配器中创建一个方法,如下所示:
public void setData(ArrayList<object> newData) {
this.data = newData;
}
然后在你的Acitivty中添加如下内容:
if(items!=null && items.size()>0) {
// First time calling adapter
if(adapter==null){
adapter = new FeedAdapter(getActivity(), items);
if(adapter!=null)
listView.setAdapter(adapter);
}
// Updating your adapter with new data.
else {
adapter.setData(yourArrayList);
adapter.notifyDataSetChanged();
}