专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Android

使用notifyDataSetChanged()无法更新视图解决思路

发布时间:2010-05-30 10:56:13 文章来源:www.iduyao.cn 采编人员:星星草
使用notifyDataSetChanged()无法更新视图
先说明一下程序实现的内容,我要将从服务器上下载的xml解析后显示在ListView上,网络请求部分单独建立线程实现,然后通过Handler,Message以字符串的形式传递回主线程。在主线程的onCreate()里listView1.setAdapter(sAdapter);待从新建的网络请求线程传递回数据后通过sAdapter.notifyDataSetChanged();更新ListVIew,但是虚拟机始终显示黑屏,没有更新视图,请大家帮忙解决(xmlParse()是解析xml的函数)

Java code
public class ReceiveXMLOnlineActivity extends Activity {
    private ListView listView1;
    private String xmlMainOnline;
    private SimpleAdapter sAdapter;
    private List<Map<String,String>> xmlList=new ArrayList<Map<String,String>>() ;
    static final int SENDXML=1;
    static final int ALERTMESSAGE=2;
    static final int ERROR=3;
        
    //消息处理
    Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch(msg.what){
            case SENDXML:
                Bundle bundleRe=new Bundle();
                bundleRe=msg.getData();
                xmlMainOnline=bundleRe.getString("xml");
                xmlList=xmlParse();
               
                sAdapter.notifyDataSetChanged();
                            //没有更新
                break;
            case ALERTMESSAGE:
                AlertDialog.Builder alertDialog=new AlertDialog.Builder(ReceiveXMLOnlineActivity.this);
                alertDialog.setMessage("请求失败!").show();
                break;
            case ERROR:
                Bundle bundleRe1=new Bundle();
                bundleRe1=msg.getData();
                String errorMessage=bundleRe1.getString("error");
                AlertDialog.Builder alertDialog1=new AlertDialog.Builder(ReceiveXMLOnlineActivity.this);
                alertDialog1.setMessage(errorMessage).show();
                break;
            }
            
        }
        
    };
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //为从网络获取XML建立新线程
        ReceiveXMLOnline newThread=new ReceiveXMLOnline();
        
        setContentView(R.layout.main);
        listView1=(ListView)findViewById(R.id.listView1);
        sAdapter=new SimpleAdapter(ReceiveXMLOnlineActivity.this,xmlList,R.layout.simpleadapter,new String[]{"FG_NAME","FG_ID"},new int[]{R.id.textView4,R.id.textView5});
        listView1.setAdapter(sAdapter);
               
        //监听ListView的单击事件
        listView1.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                switch(arg2){
                case 0:
                    changeActivity(0);
                    break;
                case 1:
                    changeActivity(1);
                    break;
                case 2:
                    changeActivity(2);
                    break;
                }
            }
            
        });
    }



------解决方案--------------------
the xmlList after downlown is not the one you used to create adapter...

they are two different reference to diferent List;

so do not use = to change the data ,

please use removeall and addall .... and the data reference does not change,rather than its elements,and the notifyDataSet will work.
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: