-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>
4

2 回答 2

2

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

于 2013-01-31T13:20:36.653 回答
1

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

希望能帮助到你!

于 2013-01-31T15:16:09.673 回答