2013-04-12 2 views
8

을 클릭 한 후 조치를 실행하는 방법은 안드로이드 문서에서 파생 다음 DialogFragment를 생성 DialogFragment를 열어야하는 목록의 행을 클릭하고 DialogFragment의 양수 버튼을 누른 후에 ListFragment의 선택된 행을 제거하고 제거와 관련된 원격 액션을 수행하는 메소드를 호출 할 수 있기를 원합니다. 내가 모르는 무엇는 DialogFragment 긍정적 버튼을

public static class ListFragment extends android.support.v4.app.ListFragment { 



     ArrayList<String> listItems=new ArrayList<String>(); 


     ArrayAdapter<String> adapter; 


     public static final String ARG_SECTION_NUMBER = "section_number"; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      final View rootView = inflater.inflate(R.layout.list_fragment_view, 
        container, false); 


      ListView lv = (ListView)rootView.findViewById(android.R.id.list); 

      }}); 
      adapter=new ArrayAdapter<String>(this.getActivity(), 
        android.R.layout.simple_list_item_1, 
        listItems); 
       setListAdapter(adapter); 
      return rootView; 
     } 



     @Override 
     public void onListItemClick(ListView l, View v, int position, long id) { 



      //opening the dialogfragment 


     } 


    } 




    } 

이 DialogFragment의 긍정적 버튼 클릭 후 작업을 처리하는 방법은 다음과 같이 은 내가 ListFragment을 구현했습니다. 너 나 좀 도와 줄 수있어?

EDIT : 명확히하기 위해 워크 플로입니다. 목록에서 -> DialogFragment 표시 -> DialogFragment를 클릭하고 목록에서 요소를 제거하십시오.

답변

1

당신이 사용할 수있는 대화 상자를 호출하려면, 당신은 PayBillDialogFragment에서 ListFragment를 호출하는 두 가지 옵션이있어

setPositiveButton("Paga", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     //Add your code here that should execute 
     //when positive button is clicked 
    } 
}); 
+0

나는 이해할 수 없다. 어떻게하면 arraylist/arrayadapter에서 onClick 메서드의 요소를 제거 할 수 있습니까? – Raffo

2

당신의 dialogFragment에서

android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 
if (fm.findFragmentByTag("PayBillDialogFragment") == null) { 
    PayBillDialogFragment payBillDialogFragment = new PayBillDialogFragment(); 
    payBillDialogFragment.show(fm, "PayBillDialogFragment"); 
} 

을.

첫 번째은 Android 가이드 라인에서 권장합니다. 모든 통신은 Activity 호스팅을 통해 이루어집니다. 그것이 ((HostingActivity)PayBillDialogFragment.getActivity()).deleteItem()PayBillDialogFragment.setPositiveButton(onClick()) 안에 전화하여 호스팅을 얻는 방법입니다. HostingActivity.deleteItem()에서 부풀려진 PayBillDialogFragment을 가져 와서 삭제 방법을 호출하십시오.

http://developer.android.com/guide/components/fragments.html#EventCallbacks

둘째를 참조하십시오. DialogFragment 개체를 만들 때 DialogFragment.setTargetFragment()을 입력하면 PayBillDialogFragment.setPositiveButton(onClick()) 안에 PayBillDialogFragment을 입력하고 DialogFragment.getTargetFragment()을 입력하고 거기에서 delete 메서드를 호출하면됩니다.

이 나는 ​​조각과 대화 조각 사이의 통신을 처리하는 방법입니다 Callback to a Fragment from a DialogFragment

11

참조

예 조각 :

public class MainFragment extends Fragment { 

    private static final int REQ_CODE = 1; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.main_fragment, container, false); 
     Button b = (Button) v.findViewById(R.id.button); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MyDialog dialog = new MyDialog(); 
       dialog.setTargetFragment(MainFragment.this, REQ_CODE); 
       dialog.show(getFragmentManager(), "dialog"); 
      } 
     }); 
     return v; 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Toast.makeText(getActivity(), "Result: " + resultCode, 
       Toast.LENGTH_SHORT).show(); 
     super.onActivityResult(requestCode, resultCode, data); 
    } 

} 

예 DialogFragment :

public class MyDialog extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage("My dialog message") 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         notifyToTarget(Activity.RESULT_OK); 
        } 
       }) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           notifyToTarget(Activity.RESULT_CANCELED); 
          } 
         }); 
     return builder.create(); 
    } 

    private void notifyToTarget(int code) { 
     Fragment targetFragment = getTargetFragment(); 
     if (targetFragment != null) { 
      targetFragment.onActivityResult(getTargetRequestCode(), code, null); 
     } 
    } 

} 

이 유일한 방법입니다 나는 또한 변화 할 때 일하고있다. 정위.

+0

저에게 이것을 보여 주셔서 감사합니다. 유용했습니다. – j2emanue

0

목록 조각은 요소를 표시하기 위해 어댑터를 사용합니다. 어댑터에는 콜렉션 형식의 입력이 필요합니다. 따라서 사용자가 대화 상자 단편에서 확인 버튼을 눌러 List Fragment로 다시 전달하면 해당 특정 요소를 Collection에서 제거하고 수정 된 Collection으로 List Fragment의 Adapter를 다시 설정할 수 있습니다.