在Android中为ListView解析XML

时间:2013-02-22 02:42:06

标签: android xml parsing listview

我正在为android listview解析xml。 我能够解析并获取listview的数据。 但是,如果我想从该XML获取特定数据,该怎么办?嗯喜欢表演 SQL中的一些内容“select * from [table] where id = 1”。 这样的事情......我怎么能用我的XML做到这一点? 感谢

2 个答案:

答案 0 :(得分:1)

public class XMLParsing_DisplayListviewActivity extends Activity {
    String res1         = null;
    InputStream is      = null;
    StringBuilder sb    = null;

    public String[] mCD;
    public String[] mTitle;
    public String[] mArtist;
    public String[] mCountry;
    public String[] mCompany;
    public String[] mPrice;
    public String[] mYear;

    ListView listview;

    ProgressDialog mDialog = null; // thread code
    private Runnable viewOrders; // thread code
    private Thread thread1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listview=(ListView)findViewById(R.id.listview);

        mDialog = new ProgressDialog(this); // thread code
        mDialog.setMessage("Please wait...");

        viewOrders = new Runnable(){
        public void run(){
            load();
            runOnUiThread(returnRes);
        }
    };
    thread1 = new Thread(null, viewOrders, "Background"); // thread
    thread1.start(); // thread code
    mDialog.show();

}

private Runnable returnRes = new Runnable(){
    public void run(){
        mDialog.cancel();
         for(int i=0;i<mCD.length;i++){
                System.out.println("======================");
                System.out.println("Title :- "+mTitle[i]);
                System.out.println("Artist :- "+mArtist[i]);
                System.out.println("Country :- "+mCountry[i]);
                System.out.println("Company :- "+mCompany[i]);
                System.out.println("Price :- "+mPrice[i]);
                System.out.println("Year :- "+mYear[i]);
            }


         listview.setAdapter(new CustomAdapter(XMLParsing_DisplayListviewActivity.this));       

    }
};

public void load() {
    //mLoading = true;
    Document doc = null;
    HttpURLConnection urlConnection = null;
    URL url = null;
    try {
        //url = new URL(urlString.toString());
        url = new URL("http://www.xmlfiles.com/examples/cd_catalog.xml");
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(urlConnection.getInputStream());
    } catch (Exception e) {
        System.out.println("e"+e.toString());
    }
    try {
        NodeList nodes = doc.getElementsByTagName("CD");
        // iterate the employees

        mCD=new String[nodes.getLength()];
        mTitle=new String[nodes.getLength()];
        mArtist=new String[nodes.getLength()];
        mCountry=new String[nodes.getLength()];
        mCompany=new String[nodes.getLength()];
        mPrice=new String[nodes.getLength()];
        mYear=new String[nodes.getLength()];

        for (int i = 0; i < nodes.getLength(); i++) {
           Element element = (Element) nodes.item(i);

           NodeList cd = element.getElementsByTagName("CD");
           Element line = (Element) cd.item(0);
           mCD[i]=getCharacterDataFromElement(line);               

           NodeList title = element.getElementsByTagName("TITLE");
           line = (Element) title.item(0);
           mTitle[i]=getCharacterDataFromElement(line);
           //System.out.println("catcatcatmidmidmidmidName: " + getCharacterDataFromElement(line));

           NodeList artist = element.getElementsByTagName("ARTIST");
           line = (Element) artist.item(0);
           mArtist[i]=getCharacterDataFromElement(line);
           //System.out.println("salary: " + getCharacterDataFromElement(line));

           NodeList country = element.getElementsByTagName("COUNTRY");
           line = (Element) country.item(0);
           mCountry[i]=getCharacterDataFromElement(line);

           NodeList company = element.getElementsByTagName("COMPANY");
           line = (Element) company.item(0);
           mCompany[i]=getCharacterDataFromElement(line);

           NodeList price = element.getElementsByTagName("PRICE");
           line = (Element) price.item(0);
           mPrice[i]=getCharacterDataFromElement(line);

           NodeList year = element.getElementsByTagName("YEAR");
           line = (Element) year.item(0);
           mYear[i]=getCharacterDataFromElement(line);

        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

  public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
       CharacterData cd = (CharacterData) child;
       return cd.getData();
    }
    return "?";
  }


  public class CustomAdapter extends BaseAdapter {
    private Context mContext;
    Application app;
    private LayoutInflater inflater=null;

    public CustomAdapter(Context c) {
        mContext = c;
         inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }   

    public int getCount() {
        return mTitle.length;

    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        if (convertView == null)        
             vi = inflater.inflate(R.layout.list, null);


        TextView txt=(TextView)vi.findViewById(R.id.txtview_title);
        TextView txt1=(TextView)vi.findViewById(R.id.txtview_artist);
        TextView txt2=(TextView)vi.findViewById(R.id.txtview_country);
        TextView txt3=(TextView)vi.findViewById(R.id.txtview_company);
        TextView txt4=(TextView)vi.findViewById(R.id.txtview_price);
        TextView txt5=(TextView)vi.findViewById(R.id.txtview_year);

        txt.setText("Title : " + mTitle[position]);
        txt1.setText("Artist : " + mArtist[position]);
        txt2.setText("Country : " + mCountry[position]);
        txt3.setText("Company : " + mCompany[position]);
        txt4.setText("Price : " + mPrice[position]);
        txt5.setText("Year : " + mYear[position]);

        return vi;
    };
  }
}

答案 1 :(得分:0)

您应该只解析一次xml文件,然后从中创建一个数组或对象列表。内存中的所有内容后,您可以使用该阵列使用ArrayAdapter填充列表。数组中的位置可以用作您的id。如果您更喜欢对象的属性是id而不是位置,那么您可以相应地对数组进行排序。