如何从AsyncTask的更新的ListView? [英] How to update ListView from AsyncTask?

查看:96
本文介绍了如何从AsyncTask的更新的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过加载图像的的AsyncTask 并更新ListView中我使用的 notifyDataSetChanged()方式。

notifyDataSetChanged()在ListView onProgressUpdate不会改变任何东西()。
我不想使用 cursor.requery ,因为它是德precated方法。

为什么 notifyDataSetChanged()工作不适合我?

 公共类新闻扩展BasicActivity实现OnItemClickListener {
私人SQLiteDatabase DB = NULL;
私人的ListView lvNews;
私人TaskImgSm taskImgSm = NULL;
私人NewsListAdapter适配器;
私人光标CUR;
布尔暂停= FALSE;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.news);    DB = SQLiteDatabase.openDatabase(((MyApp的)getApplication())getDbPath(),空,SQLiteDatabase.OPEN_READWRITE);
    lvNews =(ListView控件)findViewById(R.id.list_news);
    lvNews.setOnItemClickListener(本);
    listFilling();    如果(taskImgSm = NULL&放大器;!&安培;!taskImgSm.getStatus()= AsyncTask.Status.FINISHED)taskImgSm.cancel(真);
    taskImgSm =新TaskImgSm();
    taskImgSm.execute();
}私人无效listFilling(){
    CUR = db.query(新闻,新的String [] {_id,id_news,标题,日,img_url,img_sm},NULL,NULL,NULL,NULL,NULL) ;
    startManagingCursor(现);
    适配器=新NewsListAdapter(这一点,CUR,DB);
    lvNews.setAdapter(适配器);
    adapter.notifyDataSetChanged();
}类TaskImgSm扩展的AsyncTask<无效,字符串,太虚> {
    光标小人;
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        小人= db.query(新闻,新的String [] {_id,id_news,img_url,img_sm},NULL,NULL,NULL,NULL,NULL);
        startManagingCursor(小人);
    }    @覆盖
    保护无效doInBackground(虚空......未使用){
        curs.moveToFirst();
        而(curs.isAfterLast()== FALSE){
            如果(curs.getBlob(curs.getColumnIndex(img_sm))== NULL){
                串imgUrl的= curs.getString(curs.getColumnIndex(img_url));
                串idNews = curs.getString(curs.getColumnIndex(id_news));
                updateImg(imgUrl的,idNewsimg_sm);
                publishProgress();
            }
            curs.moveToNext();
        }
        回报(NULL);
    }    私人无效updateImg(字符串img_URL,字符串whereId,字符串imgColumn){
        尝试{
            DefaultHttpClient mHttpClient =新DefaultHttpClient();
            HTTPGET mHttpGet =新HTTPGET();
            mHttpGet.setURI(新的URI(img_URL));
            HTT presponse mHtt presponse = mHttpClient.execute(mHttpGet);
            如果(mHtt presponse.getStatusLine()的getStatus code()== HttpStatus.SC_OK){
                HttpEntity实体= mHtt presponse.getEntity();
                如果(实体!= NULL){
                    //插入数据库
                    ContentValues​​值=新ContentValues​​();
                    values​​.put(imgColumn,EntityUtils.toByteArray(实体));
                    db.update(新闻,价值观,id_news =+ whereId,NULL);
                }
            }
        }赶上(的URISyntaxException E){e.printStackTrace();
        }赶上(ClientProtocolException E){e.printStackTrace();
        }赶上(IOException异常五){e.printStackTrace();}
    }    @覆盖
    保护无效onProgressUpdate(字符串...项){
        如果(暂停== FALSE){
            adapter.notifyDataSetChanged();
        }
    }    @覆盖
    保护无效onPostExecute(虚空未使用){}
}@覆盖
保护无效的onPause(){
    暂停= TRUE;
    super.onPause();
}@覆盖
保护无效onResume(){
    暂停= FALSE;
    adapter.notifyDataSetChanged();
    super.onResume();
}@覆盖
保护无效的onDestroy(){
    如果(taskImgSm = NULL&放大器;!&安培;!taskImgSm.getStatus()= AsyncTask.Status.FINISHED)taskImgSm.cancel(真);
    super.onDestroy();
}
}


解决方案

为什么不使用ContentProvider的。

通过ContentProvider的你可以用有NotifyChange(URI)方法更新表格

教程,这里的http://thinkandroid.word$p$pss.com/2010/01/13/writing-your-own-contentprovider/

I am loading images by using AsyncTask and for updating ListView I am using notifyDataSetChanged() method.

But notifyDataSetChanged() doesn't change anything in the ListView onProgressUpdate(). I don't want to use cursor.requery because it is deprecated method.

Why notifyDataSetChanged() is not working for me?

public class News extends BasicActivity implements OnItemClickListener{
private SQLiteDatabase db = null;
private ListView lvNews;
private TaskImgSm taskImgSm = null;
private NewsListAdapter adapter;
private Cursor cur;
boolean pause = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news);

    db = SQLiteDatabase.openDatabase(((MyApp) getApplication()).getDbPath(), null, SQLiteDatabase.OPEN_READWRITE);
    lvNews = (ListView) findViewById(R.id.list_news);
    lvNews.setOnItemClickListener(this);
    listFilling();

    if (taskImgSm != null && taskImgSm.getStatus() != AsyncTask.Status.FINISHED) taskImgSm.cancel(true);
    taskImgSm = new TaskImgSm();
    taskImgSm.execute();        
}

private void listFilling() {
    cur = db.query("news", new String[] { "_id", "id_news", "title", "date", "img_url", "img_sm" }, null, null, null, null, null);
    startManagingCursor(cur);
    adapter = new NewsListAdapter(this, cur, db);
    lvNews.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

class TaskImgSm extends AsyncTask<Void, String, Void> {
    Cursor curs;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        curs = db.query("news", new String[] { "_id", "id_news", "img_url", "img_sm" }, null, null, null, null, null);
        startManagingCursor(curs);
    }

    @Override
    protected Void doInBackground(Void... unused) {
        curs.moveToFirst();
        while (curs.isAfterLast() == false) {
            if (curs.getBlob(curs.getColumnIndex("img_sm")) == null) {
                String imgUrl = curs.getString(curs.getColumnIndex("img_url"));
                String idNews = curs.getString(curs.getColumnIndex("id_news"));
                updateImg(imgUrl, idNews, "img_sm");
                publishProgress();
            }
            curs.moveToNext();
        }           
        return (null);
    }

    private void updateImg(String img_URL, String whereId, String imgColumn) {
        try {
            DefaultHttpClient mHttpClient = new DefaultHttpClient();
            HttpGet mHttpGet = new HttpGet();
            mHttpGet.setURI(new URI(img_URL));
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
            if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = mHttpResponse.getEntity();
                if (entity != null) {
                    // insert to database
                    ContentValues values = new ContentValues();
                    values.put(imgColumn, EntityUtils.toByteArray(entity));
                    db.update("news", values, "id_news=" + whereId, null);
                }
            }
        } catch (URISyntaxException e) {e.printStackTrace();
        } catch (ClientProtocolException e) {e.printStackTrace();
        } catch (IOException e) {e.printStackTrace();}
    }

    @Override
    protected void onProgressUpdate(String... item) {
        if (pause == false) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onPostExecute(Void unused) {}
}

@Override
protected void onPause() {
    pause = true;
    super.onPause();
}

@Override
protected void onResume() {
    pause = false;
    adapter.notifyDataSetChanged();
    super.onResume();
}

@Override
protected void onDestroy() {
    if (taskImgSm != null && taskImgSm.getStatus() != AsyncTask.Status.FINISHED) taskImgSm.cancel(true);
    super.onDestroy();
}
}

解决方案

Why don't use a ContentProvider.

With a ContentProvider you can update your table with the notifyChange(uri) method

Tutorial for that here http://thinkandroid.wordpress.com/2010/01/13/writing-your-own-contentprovider/

这篇关于如何从AsyncTask的更新的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆