1

나는 Activity를 통해 DialogFragment를 가지고있다. 이 DialogFragment는 웹 서비스 호출에 의해 채워지는 listview입니다. 이 목록보기에서 행을 클릭하면 대화 상자가 사라지고 '조각 인터페이스 패턴'을 통해 어댑터에서 전달 된 문자열로 활동이 재개됩니다.어댑터에서 DialogFragment를 닫으려면 어떻게합니까?

public class MyAdapter extends ArrayAdapter<Profile> implements AdapterView.OnItemClickListener { 
.... 

// Activity implements this interface to use callback and get id value 
public interface MyDialogListener { 
    void onFinishMyDialog(String id); 
} 


@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Profile selectedProfile = getItem(position); 
    String selectedProfileID = selectedProfile.getId(); 

    // fragments dont have their own context, get Activity fragment belongs to 
    Context context = getContext(); 
    MyDialogListener listener = (MyDialogListener)context; 

    listener.onFinishMyDialog(selectedProfileID); 
    /* NO ACCESS TO FRAGMENT, CANNOT DO THIS 
    dismiss(); */ 
} 

가 어떻게 대화 조각을 취소하고 제어가 다시 활동에 갈 수 있습니다 : 나는 다음과 같은 어댑터 클래스가? 그들은 정적 인 (Android - Getting context of a fragment in a pager)

답변

0

만들기 대화 상자를 가지고 있지 않기 때문에 나는 상황에서 조각을 aceess 수 없으며

가 당신에게

+1

정적 개체의 사용을 제안하지 마십시오. 활동이 파괴 될 때마다 위험합니다. –

0

도움이 될 수 있습니다 경우 당신은 어떤을 기각 할 수있을 것입니다 어댑터 생성자에서 액티비티 객체를 전달하고 어댑터에서 메서드를 호출하여 대화 상자를 닫습니다.

0

다음과 같이 생성자를 어댑터에 추가하십시오.

public class MyAdapter extends ArrayAdapter<Profile> implementsapterView.OnItemClickListener { 
    Dialog dialog; 
    .... 
    MyAdapter(Dialog dialog) { 
     this.dialog = dialog; 
    } 
} 

이제 대화 상자를 어댑터에서 닫으려고하면 dialog.dismiss()를 호출하십시오.

관련 문제