2015-01-14 1 views
0

검색된 프로세스 데이터를 목록보기로 변경하려고합니다. 데이터가 올바르게 수신됩니다. 하지만 나는 목록보기를 바르게 만들어 내지 못하고있다.asynctask에서 인터페이스가 변경되었습니다 (올바른 방법)

는 여기에 내가 내가 doInBackground에서 목록보기를 만들 수 없습니다 것을 알고 내 AsyncTask를 클래스

class GetFriendsInfo extends AsyncTask<String, String, String>{ 

    String id = ""; 
    String fullName = ""; 
    String birthday = ""; 

    protected void onPreExecute() { 
     super.onPreExecute(); 
     pd_GetData = new ProgressDialog(MainActivity.this); 
     pd_GetData.setMessage("Getting friend data"); 
     pd_GetData.setIndeterminate(false); 
     pd_GetData.setCancelable(true); 
     pd_GetData.show(); 
    } 

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

     JSONArray friendArray; 

     List<NameValuePair> param = new ArrayList<NameValuePair>(); 
     param.add(new BasicNameValuePair("user_id", id)); 
     param.add(new BasicNameValuePair("user_id", fullName)); 
     param.add(new BasicNameValuePair("user_id", birthday)); 

     JSONObject jsonObject = jsonParser.makeHttpRequest(url_get_birthdays,"GET", param); 

     try{ 
      int success = jsonObject.getInt(TAG_SUCCESS); 

      if (success == 1){ 
       Log.d("PHP Server [GET]", "Retrieved user data"); 

       String jsonString = jsonObject.getString("message"); 
       friendArray = new JSONArray(jsonString); 

       String[] names = new String[friendArray.length()]; 
       String[] birthdays = new String[friendArray.length()]; 
       String[] ids = new String[friendArray.length()]; 

       for(int i=0; i<friendArray.length(); i++) { 
        JSONObject friend = friendArray.getJSONObject(i); 
        String friend_id = friend.getString("id"); 
        ids[i] = friend_id; 
        String friend_name = friend.getString("fullName"); 
        names[i] = friend_name; 
        String friend_birthday = friend.getString("birthday"); 
        birthdays[i] = friend_birthday; 
       } 

       Log.i("friend:", Arrays.toString(ids) + " " + Arrays.toString(names) + " " + Arrays.toString(birthdays)); 

       List<HashMap<String, String>> birthday = new ArrayList<HashMap<String, String>>(); 

       for (int i=0;i<names.length;i++){ 
        HashMap<String, String> hm = new HashMap<String, String>(); 
        hm.put("names", names[i]); 
        hm.put("ids", ids[i]); 
        hm.put("birthdays", birthdays[i]); 
        birthday.add(hm); 
       } 

       String[] from = {"names", "ids", "birthdays"}; 
       int[] to = {R.id.text1, R.id.im_ProfilePic, R.id.text2}; 

       SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, birthday, R.layout.listitem_birthday, from, to); 
       HorizontalListView featuredList = (HorizontalListView) findViewById(R.id.lv_Birthdays); 
       featuredList.setAdapter(adapter); 

      }else{ 
       Log.d("PHP Server [GET]", "Failed retrieve user data"); 
      } 
     }catch (JSONException e){ 
      e.printStackTrace(); 
     }catch (RuntimeException e){ 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    protected void onPostExecute(JSONArray result) { 
     // dismiss the dialog once done 


     pd_GetData.dismiss(); 
    } 
} 

입니다. 그러나 나는 그것을 어떻게해야하는지 모른다.

+0

'onPostExecute'에서 수행 – Nasir

+0

@nasir받은 데이터를 onPostExecute로 가져 오는 방법은 무엇입니까? – DevelopingBeaver

+0

나는 대답을 게시 할 것이다 – Nasir

답변

1

이렇게하면 아이디어를 얻을 수 있습니다. 인라인 코멘트 읽기 :

class GetFriendsInfo extends AsyncTask<Void, Void, JSONObject> { 
    private String url; 
    public GetFriendsInfo(String url_get_birthdays) { 
     this.url = url_get_birthdays; 
    } 
    @Override 
    protected JSONObject doInBackground(Void... params) { 
     // Make your network call and get your JSONObject 
     JSONObject jsonObject = jsonParser.makeHttpRequest(url_get_birthdays,"GET", param); 

     return jsonObject; 
    } 

    @Override 
    protected void onPostExecute(JSONObject jsonObject) { 
     // Here you get your jsonObject on the main thread. You can parse it and update your UI 
     // Convert your jsonObject to what you want and then show the dialog 


String[] from = {"names", "ids", "birthdays"}; 
     int[] to = {R.id.text1, R.id.im_ProfilePic, R.id.text2}; 

     SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, birthday, R.layout.listitem_birthday, from, to); 
     HorizontalListView featuredList = (HorizontalListView) findViewById(R.id.lv_Birthdays); 
     featuredList.setAdapter(adapter); 

    } 
} 
1

어댑터를 onPostExecute() 메소드로 설정하십시오.

class GetFriendsInfo extends AsyncTask<String, String, String>{ 

String id = ""; 
String fullName = ""; 
String birthday = ""; 
List<HashMap<String, String>> birthday; 

protected void onPreExecute() { 
    super.onPreExecute(); 
    pd_GetData = new ProgressDialog(MainActivity.this); 
    pd_GetData.setMessage("Getting friend data"); 
    pd_GetData.setIndeterminate(false); 
    pd_GetData.setCancelable(true); 
    pd_GetData.show(); 
} 

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

    JSONArray friendArray; 

    List<NameValuePair> param = new ArrayList<NameValuePair>(); 
    param.add(new BasicNameValuePair("user_id", id)); 
    param.add(new BasicNameValuePair("user_id", fullName)); 
    param.add(new BasicNameValuePair("user_id", birthday)); 

    JSONObject jsonObject = jsonParser.makeHttpRequest(url_get_birthdays,"GET", param); 

    try{ 
     int success = jsonObject.getInt(TAG_SUCCESS); 

     if (success == 1){ 
      Log.d("PHP Server [GET]", "Retrieved user data"); 

      String jsonString = jsonObject.getString("message"); 
      friendArray = new JSONArray(jsonString); 

      String[] names = new String[friendArray.length()]; 
      String[] birthdays = new String[friendArray.length()]; 
      String[] ids = new String[friendArray.length()]; 

      for(int i=0; i<friendArray.length(); i++) { 
       JSONObject friend = friendArray.getJSONObject(i); 
       String friend_id = friend.getString("id"); 
       ids[i] = friend_id; 
       String friend_name = friend.getString("fullName"); 
       names[i] = friend_name; 
       String friend_birthday = friend.getString("birthday"); 
       birthdays[i] = friend_birthday; 
      } 

      Log.i("friend:", Arrays.toString(ids) + " " + Arrays.toString(names) + " " + Arrays.toString(birthdays)); 

      birthday = new ArrayList<HashMap<String, String>>(); 

      for (int i=0;i<names.length;i++){ 
       HashMap<String, String> hm = new HashMap<String, String>(); 
       hm.put("names", names[i]); 
       hm.put("ids", ids[i]); 
       hm.put("birthdays", birthdays[i]); 
       birthday.add(hm); 
      } 



     }else{ 
      Log.d("PHP Server [GET]", "Failed retrieve user data"); 
     } 
    }catch (JSONException e){ 
     e.printStackTrace(); 
    }catch (RuntimeException e){ 
     e.printStackTrace(); 
    } 

    return null; 
} 

protected void onPostExecute(JSONArray result) { 
    // dismiss the dialog once done 

    String[] from = {"names", "ids", "birthdays"}; 
      int[] to = {R.id.text1, R.id.im_ProfilePic, R.id.text2}; 

      SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, birthday, R.layout.listitem_birthday, from, to); 
      HorizontalListView featuredList = (HorizontalListView) findViewById(R.id.lv_Birthdays); 
      featuredList.setAdapter(adapter); 

     pd_GetData.dismiss(); 
    } 
} 
1

UI 작업은 doinbackground에서 수행 할 수 없습니다. 먼저 asyntask에서 생일 목록을 전역으로 만듭니다.

List<HashMap<String, String>> birthday = new ArrayList<HashMap<String, String>>(); // make it Global. 

이동

protected void onPostExecute(JSONArray result) { 
     // dismiss the dialog once done 

     pd_GetData.dismiss(); 

    String[] from = {"names", "ids", "birthdays"}; 
       int[] to = {R.id.text1, R.id.im_ProfilePic, R.id.text2}; 

       SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, birthday, R.layout.listitem_birthday, from, to); 
       HorizontalListView featuredList = (HorizontalListView) findViewById(R.id.lv_Birthdays); 
       featuredList.setAdapter(adapter);  
    } 

에 doinbackground에서 아래 부분은 알려 주시기 바랍니다 여전히 아니 쿼리가있는 경우.

+0

입니다. 감사합니다. 실제로 이것은 해결책이었습니다. – DevelopingBeaver

관련 문제