Android XML解析结果无法在ListView中显示

问题描述 投票:-2回答:2

我是Android软件开发的新手,并且对该网站不熟悉。我希望有人可以提供帮助。

我的Android应用程序无法将XML解析的数据显示到ListView中。仅显示Toast消息。

[帮助我,您的帮助将不胜感激。

我的代码是:.java

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

        ll = (LinearLayout) findViewById(R.id.layout);



        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if(ni.isAvailable())
        {
            if(!(ni.isConnected()))
            {
                Toast.makeText(getApplicationContext(), "No WIFI Connected...", Toast.LENGTH_LONG).show();
            }
        }
        else
        {
            /*ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if(ni.isAvailable())
            {
                if(ni.isConnected())
                {

                }
            }*/

            b = (Button) findViewById(R.id.button1);

            //intent = new Intent(getApplicationContext(), display.class);


            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub



                    ed1 = (EditText) findViewById(R.id.what);
                    ed2 = (EditText) findViewById(R.id.where);

                    String what = ed1.getText().toString();
                    String where = ed2.getText().toString();


                    if(what.equals("") || where.equals(""))
                    {
                        Toast.makeText(getApplicationContext(), "Please, Enter Some Missing Value.", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Wait... Searching.....", Toast.LENGTH_LONG).show();

                        try {

                            URL text = new URL("My_URL");

                            XmlPullParserFactory xppf=XmlPullParserFactory.newInstance();

                            XmlPullParser xpp=xppf.newPullParser();

                            xpp.setInput(text.openStream(),null);

                            int pe =xpp.getEventType();
                            boolean status=false;


                            while(pe!=XmlPullParser.END_DOCUMENT)
                            {
                                switch(pe)
                                {
                                case XmlPullParser.START_TAG:

                                    String tag=xpp.getName();
                                    if(tag.equals("status"))
                                    {
                                        status=true;
                                    }
                                    if(tag.equals("name"))
                                    {
                                        nameTag=true;
                                    }
                                    if(tag.equals("formatted_address"))
                                    {
                                        addTag=true;
                                    }
                                    break;

                                case XmlPullParser.TEXT:
                                    String value;
                                    if(status==true)
                                    {
                                        value=xpp.getText();
                                        if(value.equals("ZERO_RESULTS"))
                                        {
                                            Toast.makeText(getApplicationContext(), "No Results Found for "+ed1.getText().toString(), Toast.LENGTH_LONG).show();
                                        }
                                    }
                                    if(nameTag==true)
                                    {
                                        value=xpp.getText();
                                        newName.add(value);
                                    }
                                    if(addTag==true)
                                    {
                                        value=xpp.getText();
                                        newAdd.add(value);
                                    }

                                    break;
                                case XmlPullParser.END_TAG:
                                    String txt=xpp.getName();

                                    if(txt.equals("name"))
                                    {
                                        nameTag=false;
                                    }
                                    if(txt.equals("formatted_address"))
                                    {
                                        addTag=false;
                                    }
                                    break;

                                }//end switch

                                pe=xpp.next();
                            }//end while

                            //ArrayList to String[] for name
                            String name[] = new String[newName.size()];

                            name = newName.toArray(name);

                            //ArrayList to String[] for Address
                            String add[] = new String[newAdd.size()];

                            add = newAdd.toArray(add);

                            Intent i = new Intent(getApplicationContext(), List.class);
                            i.putExtra("nameTag", name);
                            i.putExtra("addTag", add);

                            startActivity(i);

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

                        }
                    }//else
                }//onClick
            });//listener
        }//else
    }
}

XML文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:id="@+id/linear">

   <ListView 
    android:layout_height="wrap_content" 
    android:id="@+id/listview1" 
    android:layout_width="fill_parent"/> 

</LinearLayout>
android xml-parsing android-listview
2个回答
2
投票

当我学习解析xml并将其显示在ListView中时,本教程对我有很大帮助,请看一下:http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/


1
投票

这里有一个示例,它是一个RSS提要,它使用XML,将其解析并显示在ListView中,一切都使用AsyncTask。https://github.com/Jachu5/RssFeeder_sherlock/blob/master/src/com/example/serverUtil/RSS_server_util.java

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.