android - 在重建列表视图之前删除列表视图项

时间:2012-06-06 17:14:09

标签: android list listview

我有一个列表视图,当我返回并返回列表活动时,列表会重复。需要删除第一个列表,因此只显示一个列表。任何想法???

这是我的列表活动代码:

package com.sportspubfinder;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListPubsActivity extends Activity {

    ProgressDialog ShowProgress;

    ListView lv1;

    int selectedRadius;

    Double userLat;
    Double userLong;


    public static ArrayList<SetterGetter> PubList = new ArrayList<SetterGetter>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pubslist);

        lv1 = (ListView) findViewById(R.id.listView1);

        Bundle extras = getIntent().getExtras();
        if(extras != null){
            //retrieve the passed through radius and location readings 
            selectedRadius = extras.getInt("radius_set");
            userLat = extras.getDouble("latitude_set");
            userLong = extras.getDouble("longitude_set");
        }

         //show progres bar whilst a connection is made with the url
        ShowProgress = ProgressDialog.show(ListPubsActivity.this, "",
                "Loading Pubs. Please wait...", true);

        /* --If the INAPUB api was implemented, the url would take the passed radius (selectRadius) and location of the user---
          * "http://api.inapub.co.uk/venues/geocode/"+userLat+"/"+userLong+"/"+selectedRadius+"/?API_Key=7xa3tdmjkhu6jwjvp746zgg4");*/ 
        new loadingTask().execute("http://itsuite.it.brighton.ac.uk/jp232/pubs.php");

        //check which list item has been selected
        lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Intent intent = new Intent(ListPubsActivity.this, PubActivity.class);
                //pass the pubs information through to the next activity
                intent.putExtra("pubName", PubList.get(position).getName()); 
                intent.putExtra("pubImage", PubList.get(position).getThumbnail()); 
                intent.putExtra("pubAddress", PubList.get(position).getAddress()); 
                intent.putExtra("pubTown", PubList.get(position).getTown()); 
                intent.putExtra("pubCounty", PubList.get(position).getCounty()); 
                intent.putExtra("pubDescription", PubList.get(position).getDescription()); 
                intent.putExtra("pubFacilities", PubList.get(position).getFacilities()); 
                intent.putExtra("pubRating", PubList.get(position).getRating()); 
                intent.putExtra("pubLatitude", PubList.get(position).getLatitude()); 
                intent.putExtra("pubLongitude", PubList.get(position).getLongitude());

                startActivity(intent);

            }
        });

    }

    class loadingTask extends AsyncTask<String, Void, String> {
        //check if there's an Internet connection
        /*public boolean isOnline() {
            ConnectivityManager cm =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            return cm.getActiveNetworkInfo() != null && 
               cm.getActiveNetworkInfo().isConnectedOrConnecting();
        }*/

        protected String doInBackground(String... urls) {
            //in background wait to receive the information from the ListHandler
            SAXHelper sh = null;
            try {
                sh = new SAXHelper(urls[0]);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            sh.parseContent("");
            return "";

        }

        protected void onPostExecute(String s) {
            //add Custom List Adapter once ListHandler has been received 
            lv1.setAdapter(new CustomAdapter(ListPubsActivity.this, PubList));
            //stop progress bar
            ShowProgress.dismiss();
        }
    }

    class SAXHelper {

        public HashMap<String, String> userList = new HashMap<String, String>();
        private URL url2;

        public SAXHelper(String url1) throws MalformedURLException {
            this.url2 = new URL(url1);
        }

        public ListHandler parseContent(String parseContent) {
            //add the ListHandler
            ListHandler df = new ListHandler();
            try {

                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
                xr.setContentHandler(df);
                xr.parse(new InputSource(url2.openStream()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return df;
        }

    }

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

    @Override
    protected void onStop(){
       super.onStop();
    }

}

1 个答案:

答案 0 :(得分:1)

public static ArrayList PubList = new ArrayList();

我相信,由于静态类型,您的列表内容会被追加。