I have an xml file that i am extracing data from and displaying them in the logcat window. Now i need to display them in a textview anyone can help ?

package com.example.news_services_week1;


import java.io.IOException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;


public class SportsActivity extends Activity {

    public static final String NEWS_SERVER="http://10.0.2.2/sportsNewsFeed.xml";
    private SportsTask getFromServer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sports);

        TextView txt =(TextView)findViewById(R.id.SportText);

        getFromServer= new SportsTask();
        getFromServer.execute(NEWS_SERVER,txt);



        /* ConnectivityManager ConMgr=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo netInfo = ConMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

          boolean info = netInfo.isAvailable();

           TextView txt =(TextView)findViewById(R.id.SportText);
           if(info==true){
            txt.setText("Available");
           }else
            {
             txt.setText("UnAvailable");
            }*/
    }

    private void parseXML(XmlPullParser newsBatch) throws XmlPullParserException, IOException {
        Log.i("XML Parsing", "started");
        int eventType = -1;

        //find entry records from XML
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            if (eventType == XmlResourceParser.START_TAG) {

                //get the name of the tag
                String strName = newsBatch.getName();

                if(strName.equals("Entry")) {

                    String entryNumber = newsBatch.getAttributeValue(null,"number");
                    Integer entryNum = new Integer(entryNumber);
                    String entryText = newsBatch.getAttributeValue(null,"text");
                    String entryImageUrl = newsBatch.getAttributeValue(null,"imageUrl");
                    Log.i("sports task", ">>>entryText:" + entryText);

                    }
            }
            eventType = newsBatch.next();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sports, menu);
        return true;
    }

    private class SportsTask extends AsyncTask<Object,String,Boolean>{

        @Override
        protected Boolean doInBackground(Object... params) {
            boolean result = false;

            try{

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

                NetworkInfo netInfo = ConMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

              boolean isMobileAvail = netInfo.isAvailable();
              boolean isMobileConn = netInfo.isConnected();
              boolean isRoamingConn = netInfo.isRoaming();


              String path = params[0].toString();
              Log.i("Sports Task - path", path);


              URL xmlUrl = new URL(path);
              XmlPullParser newsBatch = XmlPullParserFactory.newInstance().newPullParser();
              newsBatch.setInput(xmlUrl.openStream(),null);
              System.out.println("stream opened");

              //parse the xml
              if(newsBatch != null){
                  try{
                      parseXML(newsBatch);
                      result = true;
                     }
                  catch (XmlPullParserException e) {
                      Log.e("Exception","Pull Parser failure", e);
                  }

                 /* } catch (Exception e){
                      Log.e("Exception", "TO exception parsong XML", e);
                  }*/
              }

        }catch(Exception ex) {
            Log.e("Exception","To exception is background process",ex);
        }
        return result;
    }

            /*  boolean info = netInfo.isAvailable();

               TextView txt =(TextView)findViewById(R.id.SportText);
               if(info==true){
                txt.setText("Available");
               }else
                {
                 txt.setText("UnAvailable");
                }

               // TODO Auto-generated method stub
            return null;*/
        }





    }

Recommended Answers

All 3 Replies

TextView.setText(SOME_TEXT_HERE) what is difficult about that?

I did add these 2 line of codes on line 71. But it does not display the text..

TextView txt_news = (TextView)findViewById(R.id.Sportnews);
txt_news.append(entryText);

the problem that seems to me is that the function parseXML is called from the background thread. In order to be able to update the gui, you must do it in the main thread, or also known as the UI thread as far as i know. You should update the textview in the onPostExecute function. This function works in the main thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.