1

我从 Internet 找到了一个关于XMLListView项目的示例,我只在更新的项目中更改了 xml 链接和标签名称。应用程序工作正常,但在主屏幕上是否显示“无数据

在此处输入图像描述

原始项目在这里

编辑的项目是这样的:

主.java

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


    String xml = XMLfunctions.getXML();
    Document doc = XMLfunctions.XMLfromString(xml);


    /*
    int numResults = XMLfunctions.numResults(doc);

    if((numResults <= 0)){
        Toast.makeText(Main.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show();  
        finish();
    }

    */

    NodeList nodes = doc.getElementsByTagName("note");

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

        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);

           ///////I EDITED THESE TAGS////////////////////
        map.put("to",XMLfunctions.getValue(e, "to"));
        map.put("from", "From:" + XMLfunctions.getValue(e, "from"));
        mylist.add(map);            
    }       

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "to", "from" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  


    /*
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); 

        }
    });

    */
}
}

XMLfunctions.java

public class XMLfunctions {

public final static Document XMLfromString(String xml){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is); 

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

/** Returns element value
  * @param elem element (it is XML tag)
  * @return Element value otherwise empty String
  */
 public final static String getElementValue( Node elem ) {
     Node kid;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                 if( kid.getNodeType() == Node.TEXT_NODE  ){
                     return kid.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 public static String getXML(){  
        String line = null;

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();

              ///////I EDITED THIS LINK////////////////////
            HttpPost httpPost = new HttpPost("http://www.w3schools.com/xml/note.xml");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        }

        return line;

}

 /*

public static int numResults(Document doc){     
    Node results = doc.getDocumentElement();
    int res = -1;

    try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
    }catch(Exception e ){
        res = -1;
    }

    return res;
}

*/

public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}
}

日志猫

03-16 00:53:05.032: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-16 00:53:12.401: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-16 00:53:12.711: E/Trace(1902): error opening trace file: No such file or directory (2)
03-16 00:53:15.351: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-16 00:53:39.282: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-16 00:53:40.640: E/ActivityThread(875): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f200 that was originally bound here
03-16 00:53:40.640: E/ActivityThread(875): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f200 that was originally bound here
03-16 00:53:40.640: E/ActivityThread(875):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-16 00:53:40.640: E/ActivityThread(875):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-16 00:53:40.640: E/ActivityThread(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-16 00:53:40.640: E/ActivityThread(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-16 00:53:40.640: E/ActivityThread(875):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-16 00:53:40.640: E/ActivityThread(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-16 00:53:40.640: E/ActivityThread(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-16 00:53:40.640: E/ActivityThread(875):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
03-16 00:53:40.640: E/ActivityThread(875):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
03-16 00:53:40.640: E/ActivityThread(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-16 00:53:40.640: E/ActivityThread(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
03-16 00:53:40.640: E/ActivityThread(875):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-16 00:53:40.640: E/ActivityThread(875):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-16 00:53:40.640: E/ActivityThread(875):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-16 00:53:40.640: E/ActivityThread(875):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-16 00:53:40.640: E/ActivityThread(875):  at java.lang.Thread.run(Thread.java:856)
03-16 00:53:40.652: E/StrictMode(875): null
03-16 00:53:40.652: E/StrictMode(875): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f200 that was originally bound here
03-16 00:53:40.652: E/StrictMode(875):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-16 00:53:40.652: E/StrictMode(875):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-16 00:53:40.652: E/StrictMode(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-16 00:53:40.652: E/StrictMode(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-16 00:53:40.652: E/StrictMode(875):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-16 00:53:40.652: E/StrictMode(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-16 00:53:40.652: E/StrictMode(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-16 00:53:40.652: E/StrictMode(875):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
03-16 00:53:40.652: E/StrictMode(875):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
03-16 00:53:40.652: E/StrictMode(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-16 00:53:40.652: E/StrictMode(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
03-16 00:53:40.652: E/StrictMode(875):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-16 00:53:40.652: E/StrictMode(875):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-16 00:53:40.652: E/StrictMode(875):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-16 00:53:40.652: E/StrictMode(875):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-16 00:53:40.652: E/StrictMode(875):  at java.lang.Thread.run(Thread.java:856)
03-16 00:53:40.981: E/ActivityThread(875): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f790 that was originally bound here
03-16 00:53:40.981: E/ActivityThread(875): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f790 that was originally bound here
03-16 00:53:40.981: E/ActivityThread(875):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-16 00:53:40.981: E/ActivityThread(875):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-16 00:53:40.981: E/ActivityThread(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-16 00:53:40.981: E/ActivityThread(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-16 00:53:40.981: E/ActivityThread(875):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-16 00:53:40.981: E/ActivityThread(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-16 00:53:40.981: E/ActivityThread(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-16 00:53:40.981: E/ActivityThread(875):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
03-16 00:53:40.981: E/ActivityThread(875):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
03-16 00:53:40.981: E/ActivityThread(875):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
03-16 00:53:40.981: E/ActivityThread(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-16 00:53:40.981: E/ActivityThread(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
03-16 00:53:40.981: E/ActivityThread(875):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-16 00:53:40.981: E/ActivityThread(875):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-16 00:53:40.981: E/ActivityThread(875):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-16 00:53:40.981: E/ActivityThread(875):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-16 00:53:40.981: E/ActivityThread(875):  at java.lang.Thread.run(Thread.java:856)
03-16 00:53:40.991: E/StrictMode(875): null
03-16 00:53:40.991: E/StrictMode(875): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f790 that was originally bound here
03-16 00:53:40.991: E/StrictMode(875):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-16 00:53:40.991: E/StrictMode(875):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-16 00:53:40.991: E/StrictMode(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-16 00:53:40.991: E/StrictMode(875):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-16 00:53:40.991: E/StrictMode(875):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-16 00:53:40.991: E/StrictMode(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-16 00:53:40.991: E/StrictMode(875):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-16 00:53:40.991: E/StrictMode(875):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
03-16 00:53:40.991: E/StrictMode(875):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
03-16 00:53:40.991: E/StrictMode(875):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
03-16 00:53:40.991: E/StrictMode(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-16 00:53:40.991: E/StrictMode(875):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
03-16 00:53:40.991: E/StrictMode(875):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-16 00:53:40.991: E/StrictMode(875):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-16 00:53:40.991: E/StrictMode(875):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-16 00:53:40.991: E/StrictMode(875):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-16 00:53:40.991: E/StrictMode(875):  at java.lang.Thread.run(Thread.java:856)
03-16 00:53:46.391: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-16 00:53:46.651: E/Trace(1958): error opening trace file: No such file or directory (2)
03-16 00:53:49.301: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
4

0 回答 0