Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中为ListView解析XML_Android_Xml_Parsing_Listview - Fatal编程技术网

在Android中为ListView解析XML

在Android中为ListView解析XML,android,xml,parsing,listview,Android,Xml,Parsing,Listview,我目前正在为android listview解析xml。 我能够解析并获取listview的数据。 但是,如果我想从XML中获取特定的数据,该怎么办呢。嗯,我喜欢表演 SQL中的某些内容“从id=1的[table]中选择*”。 类似这样的东西…我如何使用XML来实现这一点? 谢谢您应该只解析xml文件一次,然后从中创建一个数组或对象列表。一旦内存中有了所有内容,就可以使用数组来填充列表。数组中的位置可以用作id。如果希望对象的属性作为id而不是位置,则可以对数组进行相应排序。public cla

我目前正在为android listview解析xml。 我能够解析并获取listview的数据。 但是,如果我想从XML中获取特定的数据,该怎么办呢。嗯,我喜欢表演 SQL中的某些内容“从id=1的[table]中选择*”。 类似这样的东西…我如何使用XML来实现这一点?
谢谢

您应该只解析xml文件一次,然后从中创建一个数组或对象列表。一旦内存中有了所有内容,就可以使用数组来填充列表。数组中的位置可以用作id。如果希望对象的属性作为id而不是位置,则可以对数组进行相应排序。

public class XMLParsing\u DisplayListviewActivity extends Activity{
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;
    };
  }
}
字符串res1=null; InputStream=null; StringBuilder sb=null; 公共字符串[]mCD; 公共字符串[]mTitle; 公共字符串[]mArtist; 公共字符串[]mCountry; 公共字符串[]mCompany; 公共字符串[]mPrice; 公共字符串[]mYear; 列表视图列表视图; ProgressDialog mDialog=null;//线程代码 私有可运行viewOrders;//线程代码 私有线程线程1; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); listview=(listview)findViewById(R.id.listview); mDialog=newprogressdialog(this);//线程代码 设置消息(“请稍候…”); viewOrders=new Runnable(){ 公开募捐{ 加载(); runOnUiThread(returnRes); } }; thread1=新线程(null,viewOrders,“Background”);//线程 thread1.start();//线程代码 mDialog.show(); } private Runnable returnRes=new Runnable(){ 公开募捐{ mDialog.cancel();
对于(int i=0;ii意味着我有自己的id..就像我必须得到所有数据只有相同的一样..在这种情况下,
id
是您自己想要识别数据的方式。它不需要是外部id或任何东西,它只是一种生成
getItemId(位置)的方法
返回一些对您有意义的内容,您可以在需要时使用这些内容来访问对象。是的,原则不变,将所有数据加载到某个数组或对象列表中。您可能可以使用GSON或Jackson来简化创建对象数组的过程。无需任何解释。只需转储代码即可。即使您经验丰富的android开发人员将知道如何处理此问题,为业余开发人员提供一些环境不会出错。