解析的xml数据无法添加到微调器适配器

时间:2014-12-22 05:51:19

标签: android xml spinner adapter xmlpullparser

这是我的主要活动。我只是无法添加元素到spinner adapter.problem是java.lang.runtime.exception,android.app.activitythread.performlaunchactivity错误等等。源代码如下:

public class MainActivity extends ActionBarActivity {

private ArrayList<String> categories;
private HandleXML obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Spinner element
    Spinner spinnerbank = (Spinner) findViewById(R.id.spinner_bank);

    // Spinner click listener
    spinnerbank.setOnItemSelectedListener((OnItemSelectedListener) this);

    ArrayList<String> categories = new ArrayList<String>();

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
            android.R.layout.simple_spinner_item, categories);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnerbank.setAdapter(dataAdapter);

}

public void onItemSelected(AdapterView <?> parent, View view, int position, long id) {
    // On selecting a spinner item
    String item = parent.getItemAtPosition(position).toString();

    // Showing selected spinner item
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

}

public void onNothingSelected(AdapterView<?>arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 public void open(View view){
     String finalUrl;
      obj = new HandleXML(finalUrl);
      obj.fetchXML();
      while(obj.parsingComplete);
      categories.add(obj.getbank());

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是我用于解析xml的HandleXML类:

public class HandleXML {
 private String bank = "Bank";
 private String urlString = null;
 private XmlPullParserFactory xmlFactoryObject;
 public volatile boolean parsingComplete = true;

 public HandleXML(String url){
      this.urlString = url;
   } 

  public String getbank(){
      return bank;
   }

  public void parseXMLAndStoreIt(XmlPullParser myParser) {
      int event;
      String text=null;
      try {
         event = myParser.getEventType();
         while (event != XmlPullParser.END_DOCUMENT) {
            String name=myParser.getName();
            switch (event){
               case XmlPullParser.START_TAG:
               break;
               case XmlPullParser.TEXT:
               text = myParser.getText();
               break;

               case XmlPullParser.END_TAG:
                  if(name.equals("Bank")){
                     bank = text;
                   }
                  else{
                   }
                  break;
                   }         
                  event = myParser.next(); 

              }
                 parsingComplete = false;
      } catch (Exception e) {
         e.printStackTrace();
      }

}

   public void fetchXML(){
          Thread thread = new Thread(new Runnable(){
             @Override
             public void run() {
                try {
                   URL url = new URL(urlString);
                   HttpURLConnection conn = (HttpURLConnection) 
                   url.openConnection();
                      conn.setReadTimeout(10000 /* milliseconds */);
                      conn.setConnectTimeout(15000 /* milliseconds */);
                      conn.setRequestMethod("GET");
                      conn.setDoInput(true);
                      conn.connect();
                InputStream stream = conn.getInputStream();

                xmlFactoryObject = XmlPullParserFactory.newInstance();
                XmlPullParser myparser = xmlFactoryObject.newPullParser();

                myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
                , false);
                myparser.setInput(stream, null);
                parseXMLAndStoreIt(myparser);
                stream.close();
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
        });

        thread.start();
       }
       }

1 个答案:

答案 0 :(得分:0)

  

问题是dataadapter无法解决。

因为您已在dataadapter方法中声明了onCreate,因此dataadapter方法无法访问onCreate。要在整个类中访问dataadapter(在其他方法中),请在类级别声明它,就像声明HandleXML obj一样:

private HandleXML obj;
private ArrayAdapter<String> dataAdapter;