当前位置:操作系统 > 安卓/Android >>

Android ListView cotent of the adapter has changed but ListView did not receive a notification

前段时间完成某项目离线数据部分时,用了大量ListView显示加载数据,在碰到大数据量的加载时容易出现的错误。即我们将List作为参数传递给ListView的Adapter,同时在开辟线程加载数据时直接对刚刚参数传递的List操作,具体代码如下:

 

[java] view plaincopyprint?
<SPAN style="FONT-SIZE: 18px">package com.jackchan.listtest; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
 
public class ListTestActivity extends Activity { 
     
    private ListView mListView; 
    private Button mBtn; 
    private int text = 0; 
    private List<String> mList; 
    private MyAdapter mAdapter; 
    private List<String> mListBak; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        mListView = (ListView)findViewById(R.id.listview); 
        mBtn = (Button)findViewById(R.id.btn); 
        mList = new ArrayList<String>(); 
  //      mListBak = new ArrayList<String>();  
        mAdapter = new MyAdapter(mList); 
        mListView.setAdapter(mAdapter); 
        mBtn.setOnClickListener(new OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                GetDataTask task = new GetDataTask(); 
                task.execute(new Void[0]); 
            } 
        }); 
    } 
     
    private class GetDataTask extends AsyncTask<Void, Void, Void>{ 
 
        @Override 
        protected Void doInBackground(Void... params) { 
        //  mListBak.clear();  
            mList.clear(); 
            for(int i = 0; i < 50; i++){ 
                try { 
                    Thread.sleep(50); 
                } catch (InterruptedException e) { 
                    // TODO Auto-generated catch block  
                    e.printStackTrace(); 
                } 
            //  mListBak.add(text+"");  
                mList.add(text+""); 
                text++; 
            } 
            return null; 
        } 
 
        @Override 
        protected void onPostExecute(Void result) { 
            super.onPostExecute(result); 
        //  mAdapter.addList(mListBak);  
            mAdapter.notifyDataSetChanged(); 
        } 
         
         
    } 
     
    private class MyAdapter extends BaseAdapter{ 
         
        private TextView mItem; 
        private List<String> mData; 
         
         
        public MyAdapter(List<String> data) { 
            this.mData = data; 
        } 
 
        @Override 
        public int getCount() { 
            // TODO Auto-generated method stub  
            return mData.size(); 
        } 
 
        @Override 
        public Object getItem(int arg0) { 
            // TODO Auto-generated method stub  
            return null; 
        } 
 
        @Override 
        public long getItemId(int position) { 
            // TODO Auto-generated method stub  
            return position; 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,