2013-02-15 4 views
0

나는 listview를 채워야 만하는 활동이 있습니다. 활동이 URL에 게시되고 응답으로 JSON을받습니다. JSON을 구문 분석하여 listview에 표시하지만 listview에 데이터가 채워지지 않습니다.비동기 작업에서 listview를 채울 수 없습니다.

활동에 대한 전체 코드 :

public class RegisterFirstActivity extends ListActivity { 

    private static final String TAG_CODE = "Code"; 
    private static final String TAG_ID = "Id"; 
    private static final String TAG_LAT = "Lat"; 
    private static final String TAG_LON = "Lon"; 
    private static final String TAG_NAME = "Name"; 


    static String response_str=null; 
    static String response_code=null; 





    String ac_code; 
    String ac_id; 
    String ac_lat; 
    String ac_lon; 
    String ac_name; 




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

    //sending async post request--------------------------------------------------------------------------------------- 
    private void sendPostRequest() { 

     class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{ 

      @Override 
      protected String doInBackground(String... params) { 



       String result = ""; 
       HttpClient hc = new DefaultHttpClient(); 
       String message; 

       //HttpPost p = new HttpPost("http://192.168.1.60/tr/MobileService/GetAC"); 
       HttpPost p = new HttpPost("http://bumba27.byethost16.com/xxxxxxx/"); 
       JSONObject object = new JSONObject(); 
       try { 

        //object.put("Id",deviceid); 
        //object.put("StringValue",value); 
//     object.put("last_name", lastname); 
//     object.put("first_name", firstname); 
//     object.put("email", email); 

       } catch (Exception ex) { 

       } 

       try { 
       message = object.toString(); 


       p.setEntity(new StringEntity(message, "UTF8")); 
       p.setHeader("Content-type", "application/json"); 
        HttpResponse resp = hc.execute(p); 
        response_code=""+ resp.getStatusLine().getStatusCode(); 
        Log.d("Response Code: ", "" + response_code); 


         InputStream inputStream = resp.getEntity().getContent(); 
         InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
         BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
         StringBuilder stringBuilder = new StringBuilder(); 
         String bufferedStrChunk = null; 

         while((bufferedStrChunk = bufferedReader.readLine()) != null){ 
          stringBuilder.append(bufferedStrChunk); 
         } 

         response_str= stringBuilder.toString(); 
         parse_json_str(response_str); 

        if (resp != null) { 
         if (resp.getStatusLine().getStatusCode() == 204) 
          result = "true"; 

         makeAToast("Response: "+resp.toString()); 
        } 


        Log.d("Status line", "" + resp.getStatusLine().getStatusCode()); 
       } catch (Exception e) { 
        e.printStackTrace(); 
        Log.i("Error in response: ",e.getMessage()); 

       } 

       return result;    

      } 

      @Override 
      protected void onPostExecute(String result) { 
       super.onPostExecute(result); 

       //Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); 

       //writeToFile(result, "record.txt"); 

       Log.i("RESPONSE",result); 


      }   
     } 

     SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(); 
     sendPostReqAsyncTask.execute();  
    } 


    //----------------------------------------------------------------------------------------------- 


    public void parse_json_str(String json_str) 
    { 
     // Hashmap for ListView 
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 



     JSONArray lJSONArray; 
     String jString = json_str; 
     try 
     { 
      lJSONArray = new JSONArray(jString); 

      JSONObject lJSONObject; 
      for (int i = 0; i < lJSONArray.length(); i++) 
      { 
       lJSONObject = lJSONArray.getJSONObject(i); 
       // PARSE FIELD HERE 
       ac_code = lJSONObject.getString(TAG_CODE); 
       Log.i("Code: ",ac_code); 
       ac_id=lJSONObject.getString(TAG_ID); 
       Log.i("Id: ",ac_id); 
       ac_lat=lJSONObject.getString(TAG_LAT); 
       Log.i("Lat: ",ac_lat); 
       ac_lon=lJSONObject.getString(TAG_LON); 
       Log.i("Lon: ",ac_lon); 
       ac_name=lJSONObject.getString(TAG_NAME); 
       Log.i("Name: ",ac_name); 
       // ETC 

      // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map.put(TAG_CODE, ac_code); 
       map.put(TAG_ID, ac_id); 
       map.put(TAG_LAT, ac_lat); 
       map.put(TAG_LON, ac_lon); 
       map.put(TAG_NAME, ac_name); 
       Log.d("map","haeflloter putting"); 
       Log.d("map",map+""); 
       // adding HashList to ArrayList 
       contactList.add(map); 
       //Log.d("tag name", contactList+""); 

      } 







         /** 
         * Updating parsed JSON data into ListView 
         * */ 
         ListAdapter adapter = new SimpleAdapter(this, contactList,R.layout.list_item,new String[] { TAG_NAME }, new int[] {R.id.name}); 
         Log.d("tag name", contactList+""); 
         setListAdapter(adapter); 


     } 
     catch(Exception e) 
     { 
      Log.d("catch", e+""); 
     } 




    } 








    public void makeAToast(String str) { 
     //yet to implement 
     Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT); 
     toast.setGravity(Gravity.CENTER, 0, 0); 
     toast.show(); 
    } 
} 

레이아웃 파일 activity_register_first :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <!-- Main ListView 
     Always give id value as list(@android:id/list) 
    --> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

list_item.xml :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <!-- Name Label --> 
     <TextView 
      android:id="@+id/name" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#43bd00" 
      android:textSize="16sp" 
      android:textStyle="bold" 
      android:paddingTop="6dip" 
      android:paddingBottom="2dip" /> 
     <!-- Description label --> 


    </LinearLayout> 

</LinearLayout> 

내가 점점 오전 오류 is :

02-15 19:26:46.154: D/catch(1429): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

나는 listview를위한 this 자습서를 따라갔습니다.

어디로 잘못 가고 있습니까? 문제를 해결하는 방법?

답변

3

AsyncTask에서 UI 스레드에서 onPreExecute(), onProgressUpdate()onPostExecute() 만 실행됩니다.

doInBackground()은 백그라운드 스레드에서 실행됩니다.

doInBackground()의 데이터를 저장 한 다음 UI를 onPostExecute()으로 업데이트해야합니다.

편집 :

이 contactList 당신의 AsyncTask를 회원 확인하고이 코드 (테스트하지) 추가 :

protected void onPostExecute(String result) { 
    super.onPostExecute(result); 

    if(mContactList != null) {  
     ListAdapter adapter = new SimpleAdapter(this, contactList,R.layout.list_item,new String[] { TAG_NAME }, new int[] {R.id.name}); 
     Log.d("tag name", contactList+""); 
     setListAdapter(adapter); 
    } 

    Log.i("RESPONSE",result); 
} 
1

UI 스레드에서 UI 변경을 수행해야합니다. 즉 onPostExecute()은 UI 스레드에서 실행됩니다.

+0

당신이 나에게 어떤 코드 또는 자습서를 제안하십시오 수 있습니다? – kittu88

+0

"android asynctask"에 대한 Google. 많은 예제가 있습니다. 그 중 하나는 다음과 같습니다. http://www.javasrilankansupport.com/2012/11/asynctask-android-example-asynctask-in.html – SimonSays

관련 문제