2013-08-17 3 views
-1

웹 서버에서 데이터베이스 (연락처 세트)에서 데이터를 검색하고 ListViews를 사용하여 데이터를 표시하려고했습니다. 내 응용 프로그램을 실행할 때마다 목록에 같은 목록 항목이 추가 될 때마다. 이미지가 더 잘 설명 할 것입니다 :응용 프로그램이 실행될 때마다 동일한 목록보기 항목이 추가되었습니다.

enter image description here

이 내 응용 프로그램을 처음 실행할 때 (내가 설치 한 후)입니다. 이제 응용 프로그램을 종료하고 다시 실행합니다. 결과는 다음과 같습니다) doInBackground (대한

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

코드는 다음과 같습니다 : 여기

enter image description here 내 응용 프로그램의 코드

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

"Neeraj, 123456789"만 포함 된 디버거를 사용하여 검사했습니다. 하지만 매번 응용 프로그램을 실행할 때마다 동일한 listview 항목이 추가됩니다. 내가 원하는 것은 단 한 번만 표시하는 것입니다. 누구든지 여기에있을 수 있니?

감사합니다,

+0

'mutualFriends'가 정적 변수 인 경우 원하는만큼 길게 유지할 수 있습니다. 런처 비동기 작업 전에 해당 목록을 정리해야합니다. –

답변

0

나는 그것이 인해 있다고 의심 할

FirstActivity.mutualFriends.add(map); 

에 당신이를 사용하여 새 항목을 추가하고 있습니다. 따라서 처음 응용 프로그램을 시작할 때는 항목이 하나뿐입니다. 두 번째로 시작하면 두 가지가 있습니다.

당신이 사용하고있는 mutualFriends이 Arraylist라고 가정합니다. 새 레코드를 추가하기 전에

FirstActivity.mutualFriends.clear(); 

을 추가하십시오.

0

한 솔루션은 지금까지 빅 데이터에 사용

또는 마지막 끝은 sqlite가에 데이터를 저장하고 얻을 수 있습니다 추가 데이터 전에 배열이 명확 또는 항상

널 ArrayList를 만들

되고 있는지 확인하십시오이다 그 데이터에서 sqlite에 데이터를 추가하기 전에, db에서 모든 레코드를 삭제하고 다시 채우기 때문에 중복 데이터가 표시되지 않습니다.

관련 문제