开发者

How to Use onresume() and onpause() for on tabclick Event

I had four tabs in my view and the tab output depend on other tab input in same view. So when I click on one tab, it generates an array list and show item in list view but when second time I click on the same tab it gives array index out of bound exception. I think it is because of previous downloaded content was in the view so how to remove/delete previous loaded content when I clicked first time on tab. I know it can be done onresume() and onpause() method but what code should be in onpause() for removing previous loaded content. For refreshing it with new content. ontabclick().

Thanks!

   public class OpeningToday extends ListActivity {
String name = null;
String typename = null;
static String array_type[];
static int totalartist;
static String array_galleryname[]=null;
static String array_address[]=null;
static String array_galleryurl[]=null;
static String array_email[];
static String array_web[];
static String array_phone[];
static String array_latitude[];
static String array_longitude[];
SitesList2 sitesList;
//static String gallerynames;
//static String addresses;
 private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i("saurabh trivedi","saurabh trivedui");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.openingtoday);
    ImageView iv=(ImageView)findViewById(R.id.imv);
    iv.setBackgroundResource(R.drawable.fiiatlogo);

  new ProgressTask6().execute();

}
 protected void onPause() {
     super.onPause();
 }

protected void onResume()
{

   super.onResume();

   new ProgressTask6().execute();

}





private class ProgressTask6 extends AsyncTask<String, Void, Boolean> {
 private ProgressDialog dialog;
            private Context context;
            @Override
            protected void onPreExecute() {
                 dialog = new ProgressDialog(OpeningToday.this);
                 dialog.setMessage("Processing...");
                 dialog.setIndeterminate(true);
                 dialog.setCancelable(false);
                 dialog.show();
        }

            @Override
        protected void onPostExecute(final Boolean success) {

                if (dialog.isShowing())
                {
                dialog.dismiss();
                setListAdapter(new MyAdapter(OpeningToday.this));
            }


           if(totalartist==0)
            {
                AlertDialog.Builder alt_bld = new AlertDialog.Builder(OpeningToday.this);
                alt_bld.setMessage("galleries not found..")
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

            }
        });

                AlertDialog alert = alt_bld.create();
                // Title for AlertDialog
                alert.setTitle("Title");
                //开发者_运维百科 Icon for AlertDialog
                alert.setIcon(R.drawable.icon);
                alert.show();
            }

     }
        @Override
        protected Boolean doInBackground(String... args) {
              try{ 
                  try {
                        SAXParserFactory spf = SAXParserFactory.newInstance();
                        SAXParser sp = spf.newSAXParser();
                        XMLReader xr = sp.getXMLReader();
                        String temp = "http://www.arteonline.mobi/iphone/output.php?visit=1";
                        temp = temp.replaceAll(" " ,"%20");
                        Log.i("temp url..",temp);
                        URL sourceUrl = new URL(temp);
                        XMLHandler myXMLHandler = new XMLHandler();
                        xr.setContentHandler(myXMLHandler);
                        xr.parse(new InputSource(sourceUrl.openStream()));
                    } 
                     catch (Exception e) {
                     System.out.println("XML Pasing Excpetion = " + e);
                    }
                     sitesList = XMLHandler.sitesList2;
                     keywordresulttab.array_galleryname = new String[sitesList.getGalleryname().size()];
                     totalartist= sitesList.getGalleryname().size();
                     Log.i("opening today artist..",Integer.toString(totalartist));
                     for (int i = 0; i < sitesList.getGalleryname().size(); i++)
                     {
                         name = sitesList.getGalleryname().get(i);
                         keywordresulttab.array_galleryname[i] = name;
                        // Log.i("array_spinner" + i, array_galleryname[i]);
                     }
                     keywordresulttab.array_type = new String[sitesList.getType().size()];
                     for (int i = 0; i < sitesList.getType().size(); i++)
                     {
                         name = sitesList.getType().get(i);
                         keywordresulttab.array_type[i] = name;
                    //     Log.i("array_spinner" + i, array_estado[i]);
                     }
                     keywordresulttab.array_email = new String[sitesList.getEmail().size()];
                     for (int i = 0; i < sitesList.getEmail().size(); i++)
                     {
                         name = sitesList.getType().get(i);
                         keywordresulttab.array_email[i] = name;
                    //     Log.i("array_spinner" + i, array_estado[i]);
                     }
                     keywordresulttab.array_address = new String[sitesList.getAddress().size()];
                     for (int i = 0; i < sitesList.getAddress().size(); i++)
                     {
                         name = sitesList.getAddress().get(i);
                         keywordresulttab.array_address[i] = name;
                         //Log.i("array_spinner" + i, array_address[i]);

                     }


                     keywordresulttab.array_web = new String[sitesList.getWeb().size()];
                     for (int i = 0; i < sitesList.getWeb().size(); i++)
                     {
                         name = sitesList.getWeb().get(i);
                         keywordresulttab.array_web[i] = name;

                     }

                     keywordresulttab.array_lati = new String[sitesList.getLati().size()];
                     for (int i = 0; i < sitesList.getLati().size(); i++)
                     {
                         name = sitesList.getLati().get(i);
                         keywordresulttab.array_lati[i] = name;
                         Log.i("array_spinner" + i,keywordresulttab.array_lati[i]);

                     }
                     keywordresulttab.array_longi = new String[sitesList.getLongi().size()];
                     for (int i = 0; i < sitesList.getLongi().size(); i++)
                     {
                         name = sitesList.getLongi().get(i);
                         keywordresulttab.array_longi[i] = name;
                         Log.i("array_spinner" + i, keywordresulttab.array_longi[i]);

                     }
                     keywordresulttab.array_phone = new String[sitesList.getPhoneno().size()];
                     for (int i = 0; i < sitesList.getPhoneno().size(); i++)
                     {
                         name = sitesList.getPhoneno().get(i);
                         keywordresulttab.array_phone[i] = name;

                     }
                     keywordresulttab.array_imgurl = new String[sitesList.getImagegalleryurl().size()];
                     for (int i = 0; i < sitesList.getImagegalleryurl().size(); i++)
                     {
                         name = sitesList.getImagegalleryurl().get(i);
                         keywordresulttab.array_imgurl[i] = name;

                     }
         } catch (Exception e){
           Log.e("tag", "error", e);
           return false;
        }
        return null;
       }
        class MyAdapter extends BaseAdapter implements OnClickListener
        {

                    private LayoutInflater inflater;

                    public MyAdapter(Context ctx) {
                        super();
                        this.inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    }        

                    @Override
                    public int getCount() {
                        return totalartist;
                    }

                    /* Not implemented but not really needed */
                    @Override
                    public Object getItem(int position) {
                        return null;
                    }

                    /* Not implemented but not really needed */
                    @Override
                    public long getItemId(int position) { 
                        return 0;
                    }

                    @Override
                    public View getView(int position, View ConvertView, ViewGroup parent) 
                    {
                        View v = inflater.inflate(R.layout.listitem_layout, parent, false);
                       // Log.i("array galoijewdh..",keywordresulttab.array_galleryname[position]);
                        Log.i("saurabh trivedi","saurabh trivedui");
                       // Variables.a=3;

                        String   gallerynames = keywordresulttab.array_galleryname[position];
                        String  addresses = keywordresulttab.array_address[position];            
                        TextView tv = (TextView) v.findViewById(R.id.barrio);
                        tv.setText(gallerynames);
                        tv = (TextView) v.findViewById(R.id.ciudad);
                        tv.setText(addresses);
                        ((BaseAdapter)(getListAdapter())).notifyDataSetChanged();
                        return v;
                    }
                @Override
                    public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
                    }
                    }
    }
}


Make your array list empty on onResume() method and fill it by new content now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜