Search code examples
androidxml-parsingandroid-listview

Android XML parsing Result cant display in ListView


I am new to Android software development and new to this site. I hope someone can help.

My Android Application cant able to display XML parsed data into ListView.. Its displaying Toast message only.

Help me, Your help would be appreciated.

My Code Is : .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 file:

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

Solution

  • when I was learning parsing xml and displaying it in ListView this tutorial helped me a lot, take a look: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/