0

我目前正在为 android listview 解析 xml。我能够解析并获取我的列表视图的数据。但是,如果我想从该 XML 中获取特定数据怎么办。嗯,就像在 SQL“select * from [table] where id = 1”中执行某些操作一样。类似的东西......我怎样才能用我的 XML 做到这一点?谢谢

4

2 回答 2

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;
    };
  }
}
于 2013-04-24T08:23:34.427 回答
0

您应该只解析 xml 文件一次并从中创建一个数组或对象列表。一旦你把所有的东西都放在内存上,你就可以使用数组来用ArrayAdapter填充你的列表。数组中的位置可以作为你的id。如果您希望对象的属性是 id 而不是位置,那么您可以相应地对数组进行排序。

于 2013-02-22T02:56:05.013 回答