2013-04-12 24 views
8

tôi tạo ra các DialogFragment sau bắt nguồn từ tài liệu Android:Làm thế nào để thực hiện hành động sau DialogFragment nút tích cực nhấp

public class PayBillDialogFragment kéo dài DialogFragment {

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState){ 

     final Bundle b = this.getArguments(); 
     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage("Paga bollettino") 
       .setPositiveButton("Paga", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // FIRE ZE MISSILES! 


        } 
       }) 
       .setNegativeButton("Cancella", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 

    } 





} 

Từ một Fragment (một ListFragment), khi một hàng của danh sách được nhấn vào DialogFragment sẽ được mở và sau khi nhấn nút tích cực của DialogFragment tôi muốn có thể loại bỏ hàng đã chọn của ListFragment và cũng gọi một phương thức để thực hiện một hành động từ xa liên quan đến việc loại bỏ. tôi thực hiện các ListFragment như sau:

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 


     } 


    } 




    } 

Những gì tôi không biết là làm thế nào để xử lý các hành động sau khi click vào nút tích cực của DialogFragment. Bạn có thể giúp tôi được không?

EDIT: để làm rõ, đây là quy trình làm việc: nhấp vào danh sách -> hiển thị DialogFragment -> sau khi nhấp vào DialogFragment xóa phần tử khỏi danh sách.

Trả lời

1

Để gọi hộp thoại, bạn có thể sử dụng này:

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

Trong dialogFragment của bạn,

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

Tôi không hiểu. Làm cách nào để tôi có thể xóa các phần tử khỏi arraylist/arrayadapter khỏi phương thức onClick? – Raffo

2

Bạn đã có hai tùy chọn để gọi ListFragment từ PayBillDialogFragment.

Đầu tiên là được khuyến cáo theo nguyên tắc của Android. Tất cả các thông tin liên lạc đi qua lưu trữ Activity. Đó là cách bạn nhận được lưu trữ Activity bằng cách gọi ((HostingActivity)PayBillDialogFragment.getActivity()).deleteItem() bên trong PayBillDialogFragment.setPositiveButton(onClick()). Trong HostingActivity.deleteItem(), hãy tăng PayBillDialogFragment và gọi một số phương thức xóa trong đó.

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

Second. Bạn chỉ có thể DialogFragment.setTargetFragment() khi tạo đối tượng DialogFragment và sau đó bên trong PayBillDialogFragment.setPositiveButton(onClick()) bạn có thể nhận được PayBillDialogFragment theo DialogFragment.getTargetFragment() và gọi phương thức xóa ở đó.

Xem Callback to a Fragment from a DialogFragment

11

Đây là cách tôi xử lý thông tin liên lạc giữa các đoạn và đoạn thoại

đoạn Ví dụ:

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); 
    } 

} 

Ví dụ 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); 
     } 
    } 

} 

Đây là phương pháp duy nhất Tôi cũng làm việc khi thay đổi sự định hướng.

+0

cảm ơn bạn đã cho tôi biết điều này. nó rất hữu ích. – j2emanue

0

Phân đoạn danh sách sẽ sử dụng Bộ điều hợp để hiển thị các phần tử. Bộ điều hợp yêu cầu đầu vào dưới dạng một số Bộ sưu tập. Vì vậy, những gì bạn có thể làm là, khi người dùng nhấn nút ok trên đoạn hộp thoại và bạn liên lạc lại với List Fragment, bạn có thể xóa phần tử cụ thể đó khỏi Bộ sưu tập và đặt lại Bộ điều hợp danh sách với bộ sưu tập đã sửa đổi.

Các vấn đề liên quan