刷新ListActivity时发生adapter.notifyDataSetChanged()问题

时间:2011-10-19 08:14:49

标签: android

请参阅以下代码。在以下代码中,我想在点击按钮时从服务器下载一些数据。

  public void fetch1(){

                event=this;

                // TODO Auto-generated method stub
                try {

                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        try{

          //dialog.show();


          BufferedReader in = null;





          String url=URLManager.orderUrl+DataFactory.userData.sid+"&dnumber="+DataFactory.userData.dnumber+"&from_date="+fromDateStr+"&to_date="+toDateStr; ;

         // String login = "morris";
          //String pass = "mor$ris";

          DefaultHttpClient client = new DefaultHttpClient();
          HttpGet request = new HttpGet();


          client.getCredentialsProvider().setCredentials(new AuthScope("newdev.objectified.com", 80), new UsernamePasswordCredentials(URLManager.user, URLManager.pass));

          request.setURI(new URI(url));
          HttpResponse response = client.execute(request);

                 in = new BufferedReader
                 (new InputStreamReader(response.getEntity().getContent()));
                 StringBuffer sb = new StringBuffer("");
                 String line = "";
                 String NL = System.getProperty("line.separator");
                 while ((line = in.readLine()) != null) {
                     sb.append(line + NL);
                 }
                 in.close();
                 String page = sb.toString();
              //  Toast.makeText(event,url+"--::::--"+ page, Toast.LENGTH_LONG).show();


                  OrdersHandler handler=new OrdersHandler();
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
                xr.setContentHandler(handler);
                InputSource is = new InputSource();
                  is.setCharacterStream(new StringReader(page));           
                  xr.parse(is);  
                  Vector v=handler.getData();
                  DataFactory.ordersVector=v;


                 dataVector=DataFactory.ordersVector;

                 ordersId=new String[dataVector.size()];
                for(int a=0;a<dataVector.size();a++){
                    OrdersData o=(OrdersData)dataVector.elementAt(a);
                    ordersId[a]=o.order_id;



                }// end of the for


                //event.setListAdapter(new IconicAdapter());

            //  adapter.clear();
                // Create an ArrayAdapter, that will actually make the Strings above
                // appear in the ListView






                  /*
                 for(int a=0;a<v.size();a++){
                    OrdersData o=(OrdersData)v.elementAt(a);
                    names=names+o.order_id+"\n";


                 }// end of the for loop

                //String data=d.sid+"\n"+d.dtitle+"\n"+d.dcountry+"\n"+d.dcity;
                  Toast.makeText(event, names, Toast.LENGTH_LONG).show();
                   */


                                        }catch(Exception e){


                                        }
                                        }
                                });
                            }

                    };
                    new Thread(runnable).start();



                    //executeHttpGet();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                     Toast.makeText(this, e+"", Toast.LENGTH_SHORT).show();
                }
                adapter.notifyDataSetChanged();

                adapter.clear();
                adapter=new IconicAdapter();
                this.setListAdapter(adapter);

            }// end of the fetch method

我想在使用上述方法下载数据时刷新ListActivity。但不幸的是app崩溃了。请帮助我是android开发新手

1 个答案:

答案 0 :(得分:0)

adapter.notifyDataSetChanged();
adapter.clear();
adapter=new IconicAdapter();
this.setListAdapter(adapter);

如果之前没有设置适配器,则在空指针上运行notifyDataSetChanged()和clear()。如果它已经创建,你创建一个新的适配器和setListAdapter()anwyay:调用notifyDataSetChanged()将是没有意义的(旧的适配器不再显示在列表中,新的适配器是),而clear()仅用于你需要做一些清理工作。

我认为您正在尝试显示ordersId []中包含的字符串列表?但是你没有把它传递给IconicAdapter。 IconicAdapter是内部类吗?

查看http://developer.android.com/reference/android/os/AsyncTask.html - 首选线程。