在Android中重新加载活动时出现问题

时间:2012-04-28 05:15:32

标签: android android-activity

我已经开发了Android RSS reader。我创建了一个标签布局,并为每种类型的新闻创建了每个标签(例如标题,国家,国际等)。

然后我在tablayout的第一页上的自定义列表视图中列出了RSS标题及其图像。

见下图。

here is the screenshot

现在,当用户点击新闻标题时,将打开另一个活动(页面),其中包含新闻说明。

我的问题是当用户点击标签并加载时,如果互联网连接不可用,我想显示一个带有两个按钮的对话框Retry& Exit

当用户点击Retry时,应重新加载活动。但在我的代码中,它仅重新加载当前活动,并仅显示该活动而非仅显示包含tablayout的主活动。

以下是我的代码。

    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(); 
  }
 }
}

感谢。

0 个答案:

没有答案