1

我是一名新的 Android 程序员,最近,

我正在使用in解析xml文件。但我没有这样做。请建议我如何做到这一点。pull parserListView

我的实际代码是

MainActivity.java

package com.example.simplexmlpullapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    ListView list;
    TextView text1;
    String str;
    ArrayList<Data> arrdata;
    ArrayList<String> arrayofString, parcedCountry;
    String textforinput = "<foo>Hello World!</foo>";
    String parsedData = "";
    XmlPullParser xmlPullParser;
    public myAdapter adapter;

    @Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//      text1=(TextView)findViewById(R.id.text1);
        list=(ListView)findViewById(R.id.li);

        arrayofString = new ArrayList<String>();
        parcedCountry = new ArrayList<String>();


        try
         {
                 InputStream is;
                 is=getAssets().open("countries.xml");
                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
                 StringBuilder total = new StringBuilder();
                 String line;
                 while ((line = r.readLine()) != null)
                 {
                         total.append(line);
                 }



                 parseCountries(total.toString());

//                 ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, 
//                         android.R.layout.simple_list_item_1,parcedCountry); 
//                 list.setAdapter(adapter);
          }
         catch (Exception e) 
         {
                 // TODO: handle exception
         }

        arrayofString=new ArrayList<String>();

    }



private void parseCountries(String string)
{

        int eventType=0;
        String countries = null;
        boolean countryFlag=false;
        ArrayList<String>parsedCountry=new ArrayList<String>();
        try 
        {
                xmlPullParser=XmlPullParserFactory.newInstance().newPullParser();
                xmlPullParser.setInput(new StringReader(string));
                eventType = xmlPullParser.getEventType();
        }
        catch (XmlPullParserException e) {
                e.printStackTrace();
        }

        while(eventType!=XmlPullParser.END_DOCUMENT)
        {
                switch (eventType) 
                {
                case XmlPullParser.START_DOCUMENT:
                        break;

                case XmlPullParser.START_TAG:
                        if(xmlPullParser.getName().equalsIgnoreCase("country"))
                        {
                                countryFlag=true;        
                        }
                        break;
                case XmlPullParser.TEXT:

                        if(countryFlag)
                        {
                                countries=xmlPullParser.getText().toString().trim();
                                Log.i("parse_example", "country"+countries); 
                        }        
                        break;

                case XmlPullParser.END_TAG:
                        if(xmlPullParser.getName().equalsIgnoreCase("country"))
                        {
                                parsedCountry.add(countries);
                                countryFlag=false;        
                        }
                        break;
                        //end of switch        
                }

                try {
                    eventType=xmlPullParser.next();

//                   for(int i=0;i<arrdata.size();i++)
//                      {
//
//                          Data data=arrdata.get(i);
//                          arrayofString.add(data.getCountry());
//                      } 

                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }


}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


public class myAdapter extends ArrayAdapter<String>
{
private final Context context;
    int id;
    ArrayList<String> arrayList=new ArrayList<String>();
    public myAdapter(Context context, int textViewResourceId,
            ArrayList<String> objects) {

        super(context, textViewResourceId, objects);
        this.context=context;
        id=textViewResourceId;
        arrayList=objects;
        // TODO Auto-generated constructor stub
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return super.getCount();
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return super.getItemId(position);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return super.getView(position, convertView, parent);
        myAdapter adapter=new myAdapter(this,android.R.,parcedCountry); 
        list.setAdapter(adapter);

            }
    }

}

MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/li"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000"
        >
    </ListView>

</LinearLayout>
4

1 回答 1

0

看看这个使用 XML 解析器的例子:也许你会找到一些帮助。

于 2013-09-13T09:57:33.153 回答