2013-05-05 4 views
3

내 활동의 AsyncTask이 완료되었을 때 목록 조각을 업데이트하려고하는데 잘못된 작업을하고 있는지 확실하지 않습니다. 현재, 나는 시작 버튼이있는 AsyncTask :AsyncTask 이후에 안드로이드 새로 고침 조각보기

search = (Button)findViewById(R.id.search); 
    search.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        String productname = prodname.getText().toString().trim(); 
        if (NetworkManager.isOnline(getApplicationContext())){ 
         // Go to Other screen 
         AsyncFetcher results = new AsyncFetcher(currActivity); 
         String _url = "http://192.168.1.3:3000/search.json? 
             utf8=%E2%9C%93&q="+productname; 
         // progressDialog = ProgressDialog.show(
         // ClassifiedsActivity.this, "", "Loading..."); 
         results.execute(_url); 
        } else { 
         // Throw some warning saying no internet 
         // connection was found 
        } 
       } 
      }); 

일단 내 내 활동 내에서 조각을 인스턴스화 다음 한 완료 실행 : 그것은 같지 않습니다 그러나

ResultFragment resultfrag = new ResultFragment(); 
getSupportFragmentManager().beginTransaction() 
          .replace(R.id.content_frame, resultfrag).commit(); 

이 함께 내 콘텐츠를 교체 목록 조각. 조각을 교체하는 것은 그것을 상쾌처럼 아무것도

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

답변

12

첫째 : 여기 내 레이아웃 파일입니다. 예, 모든보기 및 데이터를 다시로드합니다. 그러나 새로운 데이터와 관련시켜야합니다. 그래서 당신의 조각에 새로 고침 메서드를 만들고이 메서드를 새로 고친 데이터로 보내도록 권고 한 다음 어댑터에 dataSetChanged으로 알리십시오.

이렇게하려면 현재 연결되어있는 조각에 도달하여 새로 고침 메서드를 호출해야합니다. findFragmentByTag을 사용하여 연락 할 수 있습니다.

편집 : 당신의 AsyncTask가 완료되면 더 조금

, 당신이 onPostExecute 방법 같은 것을해야 명확히하려면 :

ResultFragment resultFrag = (ResultFragment) getSupportFragmentManager() 
           .findFragmentByTag("FragToRefresh"); 
if (resultFrag != null) { 
    resultFrag.refreshData(refreshedArray); 
} 

그리고 당신의 ResultFragment 당신이 refreshData 방법이 필요합니다, 이는 다음과 같습니다.

public void refreshData(ArrayList<YourObject> data) { 
    yourArray = new ArrayList<YourObject>(data); 
    yourAdapter.notifyDataSetChanged(); 
} 

그리고 귀하의 목록은 귀하의 작업이 완료 될 때마다 새로 고쳐집니다.

+0

안녕하세요, 감사합니다. 난 당신이 여기에 설명 된 방법을 추가하고도 추가했다 "m_adapter.clear(); \t \t m_adapter.notifyDataSetChanged(); \t \t m_adapter.addAll (this.search_results를);" 어댑터가 여전히 이전 "ArrayList"에 대한 참조를 보유하고 있으므로 새로 고침 데이터가 작동하도록 만듭니다. – user2352691

+0

아 물론 그렇습니다. listView 및 해당 어댑터를 사용하여 처리하는 방법에 따라 다릅니다. 나는 네가 한 것을 기쁘게 생각한다. :) – yahya

관련 문제