Android 每次应用程序运行时都会添加相同的listview项

Android 每次应用程序运行时都会添加相同的listview项,android,android-listview,Android,Android Listview,我尝试从web服务器上的数据库(联系人集)检索数据,并尝试使用ListView显示数据。每当我运行应用程序时,每次它都会将相同的列表项添加到列表中。这张图片将更好地解释这一点: 这是我第一次运行我的应用程序(安装后)。现在我退出应用程序并再次运行它。结果是: 以下是我的申请代码: RegisterMe reg=connect.new RegisterMe(); //RegisterMe is an asynchronous task used reg.execute(myPhoneNu

我尝试从web服务器上的数据库(联系人集)检索数据,并尝试使用ListView显示数据。每当我运行应用程序时,每次它都会将相同的列表项添加到列表中。这张图片将更好地解释这一点:

这是我第一次运行我的应用程序(安装后)。现在我退出应用程序并再次运行它。结果是:

以下是我的申请代码:

RegisterMe reg=connect.new RegisterMe(); //RegisterMe is an asynchronous task used
    reg.execute(myPhoneNumber); // to receive data. execute() fxn is called 
                                // which invokes doInBackground() fxn.
    /*try{
        Thread.sleep(3000);
    }catch(Exception e)
    {
        System.out.println(e.toString());
    } */


      adapter=new SimpleAdapter(this,mutualFriends,R.layout.activity_first,new String[]{KEY_NAME,KEY_NUMBER}
                                ,new int[]{R.id.text1,R.id.text2});
    setListAdapter(adapter); 
doInBackground()的代码如下:

protected class RegisterMe extends AsyncTask<String,Void,String>
{
protected String doInBackground(String... str)
{
        String myPhoneNumber;
        myPhoneNumber=str[0];
        InputStream is = null;
        String result=new String();
        HttpClient httpclient=new DefaultHttpClient();
        HttpPost httppost=new HttpPost("http://johnconnor.comuf.com/register.php");
        try
        {
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("mynumber",myPhoneNumber));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response= httpclient.execute(httppost);
            Log.i("postData",response.getStatusLine().toString());
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch (ClientProtocolException e) {
            Log.e("log_tag", "Error in http connection "+e.toString());
        } catch (IOException e) {
           }
        try {
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
        } 

        result=result.substring(0,result.indexOf('\n'));
        return result ;  //result contains "Neeraj,"123456789"
}
protected void onPostExecute(String result)
{

    String[] array;
    array=result.split("[/]");

    HashMap<String,String> map=new HashMap<String,String>();
    for(int i=0;i<array.length-1;i++)
    {
        String[] singleContact;
        singleContact=array[i].split("[,]");
        map.put(FirstActivity.KEY_NAME,singleContact[0]);
        map.put(FirstActivity.KEY_NUMBER, singleContact[1]);
        FirstActivity.mutualFriends.add(map);
    }
    FirstActivity.adapter.notifyDataSetChanged();
}
受保护类注册表扩展异步任务
{
受保护的字符串doInBackground(字符串…str)
{
字符串myPhoneNumber;
myPhoneNumber=str[0];
InputStream=null;
字符串结果=新字符串();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://johnconnor.comuf.com/register.php");
尝试
{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“mynumber”,myPhoneNumber));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
Log.i(“postData”,response.getStatusLine().toString());
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(客户端协议例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}捕获(IOE异常){
}
试一试{
缓冲读取器=
新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
} 
result=result.substring(0,result.indexOf('\n'));
返回结果;//结果包含“Neeraj”,123456789
}
onPostExecute的代码如下所示:

protected class RegisterMe extends AsyncTask<String,Void,String>
{
protected String doInBackground(String... str)
{
        String myPhoneNumber;
        myPhoneNumber=str[0];
        InputStream is = null;
        String result=new String();
        HttpClient httpclient=new DefaultHttpClient();
        HttpPost httppost=new HttpPost("http://johnconnor.comuf.com/register.php");
        try
        {
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("mynumber",myPhoneNumber));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response= httpclient.execute(httppost);
            Log.i("postData",response.getStatusLine().toString());
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch (ClientProtocolException e) {
            Log.e("log_tag", "Error in http connection "+e.toString());
        } catch (IOException e) {
           }
        try {
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
        } 

        result=result.substring(0,result.indexOf('\n'));
        return result ;  //result contains "Neeraj,"123456789"
}
protected void onPostExecute(String result)
{

    String[] array;
    array=result.split("[/]");

    HashMap<String,String> map=new HashMap<String,String>();
    for(int i=0;i<array.length-1;i++)
    {
        String[] singleContact;
        singleContact=array[i].split("[,]");
        map.put(FirstActivity.KEY_NAME,singleContact[0]);
        map.put(FirstActivity.KEY_NUMBER, singleContact[1]);
        FirstActivity.mutualFriends.add(map);
    }
    FirstActivity.adapter.notifyDataSetChanged();
}
受保护的void onPostExecute(字符串结果)
{
字符串[]数组;
array=result.split(“[/]”);
HashMap=newHashMap();

对于(inti=0;i我怀疑这是由于

您正在使用此添加一个新条目。因此,第一次启动应用程序时,只有一个条目。第二次启动时,有两个条目

假设您使用的
mutualFriends
是Arraylist。请尝试添加

FirstActivity.mutualFriends.clear(); 

在添加新记录之前。

一个解决方案是确保阵列是清晰的 或

在添加数据之前始终使arraylist为空

或 用于大数据的最后一个目的是将数据存储到sqlite中并从中获取数据
在sqlite中添加数据之前,请删除数据库中的所有记录并重新填充,这样它就不会显示重复的数据。

如果
mutualFriends
是一个静态变量,则可以按照您的需要将其保持更长的时间。您需要在启动异步任务之前清除该列表。