웹 서버에서 데이터베이스 (연락처 세트)에서 데이터를 검색하고 ListViews를 사용하여 데이터를 표시하려고했습니다. 내 응용 프로그램을 실행할 때마다 목록에 같은 목록 항목이 추가 될 때마다. 이미지가 더 잘 설명 할 것입니다 :응용 프로그램이 실행될 때마다 동일한 목록보기 항목이 추가되었습니다.
이이 내 응용 프로그램을 처음 실행할 때 (내가 설치 한 후)입니다. 이제 응용 프로그램을 종료하고 다시 실행합니다. 결과는 다음과 같습니다) 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);
코드는 다음과 같습니다 : 여기
내 응용 프로그램의 코드
다음과 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 항목이 추가됩니다. 내가 원하는 것은 단 한 번만 표시하는 것입니다. 누구든지 여기에있을 수 있니?
감사합니다,
'mutualFriends'가 정적 변수 인 경우 원하는만큼 길게 유지할 수 있습니다. 런처 비동기 작업 전에 해당 목록을 정리해야합니다. –