0

I have develped an Android RSS reader.I have created a tab layout and created each tab for each type of news(say Headlines,National,International etc).

Then I listed RSS title and its images in custom listview on the first page of tablayout.

see below image.

here is the screenshot

Now when the user click on a news title, another activity(page) opens with the news description.

My problem is when a user clicks on a tab and when it loads, if the internet connection is not available I want to show a dialog box with two buttons, Retry & Exit.

When the user click Retry the activity should be reloaded.But in my code,it reloads only the current activity and displays only that activity not main activity which contains tablayout.

Below is my code.

    public class International extends Activity {
    static final String URL = "http://www.abcd.com/en/taxonomy/term/3/0/feed";
    static final String KEY_HEAD = "item";
    static final String KEY_DATE = "pubDate";
    ListView list;
    InternationalAdapter adapter;

    public static String[] Title;
    public static String[] Description;
    public static String[] image;
    public static String[] Date;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.headlines);

        Bundle mybundle = new Bundle();
        mybundle.putString("number", "0");
        new DoInBackground().execute();
    }
    public void do_update() 
        {
            internationalparser.parse();//this is the function to parse RSS feeds

        }


    public void populate_listview()
     {

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

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_HEAD);
        // looping through all song nodes <song>
        NodeList itemLst = doc.getElementsByTagName("item");


        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            //map.put(KEY_DATE, parser.getValue(e, KEY_DATE));

            newsList.add(map);
        }


        list=(ListView)findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new InternationalAdapter(this, newsList);        
        list.setAdapter(adapter);


        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                Intent myintent = new Intent("com.abcd.rssreaderinternationalpodcast.PODCAST");
                Bundle mybundle = new Bundle();
                mybundle.putInt("number", position);
                myintent.putExtras(mybundle);

                startActivity(myintent);

            }

        }); 

    }


     private class DoInBackground extends AsyncTask<Void, Void, Void>
     implements DialogInterface.OnCancelListener
{   
private ProgressDialog dialog;
private Intent intent;
private Intent intent2;

public void onPreExecute() 
{

    dialog = ProgressDialog.show(International.this, "", "Loading", true);

}

protected Void doInBackground(Void... unused) 
{ 


do_update(); 
return null; 
}

public void retry()
{

    internationalparser.parse();
}

protected void onPostExecute(Void unused) 
{ 


    if(International.Title!=null)
    {

        dialog.dismiss();
        populate_listview();

    }
    if(International.Title==null)
    {
        dialog.dismiss();
         AlertDialog.Builder alertbox = new AlertDialog.Builder(International.this);

          alertbox.setMessage("Error in connection!");
          alertbox.setPositiveButton("Retry", new DialogInterface.OnClickListener() {

             public void onClick(DialogInterface arg0, int arg1) {

               retry();             //here i want to reload  activity

             }

         });

         alertbox.setNegativeButton("Exit", new DialogInterface.OnClickListener() {


             public void onClick(DialogInterface arg0, int arg1) {

                 finish();

             }

         });

         alertbox.show();

    }

}

public void onCancel(DialogInterface dialog) 
{ 
cancel(true); 
dialog.dismiss(); 
  }
 }
}

Thanks.

Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
  • Did you follow [this](http://stackoverflow.com/questions/1397361/how-do-i-restart-an-android-activit) – Rafiq Apr 28 '12 at 05:25
  • I used those methods.But it reloads and desplays only that current activity.Not main activity. – Basim Sherif Apr 28 '12 at 05:48
  • I have one more doubt, is there is a better way to check the internet connection??...because the method I used is not so good... – Basim Sherif Apr 28 '12 at 06:04
  • Do you want like this:dialog.cancel(); International.this.finish(); startActivity(new Intent(getApplicationContext(), MainActivity.class)); – Rafiq Apr 28 '12 at 06:15
  • You can check your device's internet connectivity using:private boolean isNetworkAvailable(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); // return cm.getActiveNetworkInfo().isConnectedOrConnecting(); if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) { // Internet Connection Is Available return true; } else { // Internet Connection Is Not Present return false; } } – Rafiq Apr 28 '12 at 06:25
  • I tried your code, it reloads and displays the first tab.I want to reload and go to the same tab the user clicked when the internet connection was not available.Thank you.. – Basim Sherif Apr 28 '12 at 06:25

0 Answers0