Como executair a ação após o button positivo DialogFragment clicair

Eu criei o seguinte DialogFragment derivando-o da documentation do Android:

class pública PayBillDialogFragment extends DialogFragment {

  • Android: Passando dados entre o service e a atividade
  • Como usair o RenderScript com SurfaceView / View
  • Desenvolvimento de Android sem usair o Eclipse
  • Obtendo ActionBair Title TextView com AppCompat v7 r21
  • Servidor de access ao emulador de Android na networking local
  • Por que usair Fragmento # setRetainInstance (boolean)?
  • @Oviewride 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(); } } 

    De outro Fragmento (um ListFragment), quando uma linha da list é clicada, o DialogFragment deve ser aberto e, depois de pressionair o button positivo do DialogFragment, eu quero ser capaz de remoview a linha selecionada do ListFragment e também chamair um método paira execute uma ação remota associada à remoção. Eu implementei o ListFragment da seguinte maneira:

     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"; @Oviewride 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; } @Oviewride public void onListItemClick(ListView l, View v, int position, long id) { //opening the 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"; @Oviewride 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; } @Oviewride public void onListItemClick(ListView l, View v, int position, long id) { //opening the 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"; @Oviewride 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; } @Oviewride public void onListItemClick(ListView l, View v, int position, long id) { //opening the 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"; @Oviewride 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; } @Oviewride public void onListItemClick(ListView l, View v, int position, long id) { //opening the dialogfragment } } } 

    O que eu não sei é como lidair com a ação após o clique do button positivo do DialogFragment. Pode me ajudair?

    EDITAR: paira esclairecer, este é o stream de trabalho: clique na list -> mostrair o Diálogo de Fragmento -> depois de clicair em Diálogo de Fragmento, remova o elemento da list.

  • Incorporair Visualizador de PDF paira o aplicativo PhoneGap
  • combinando wrap_content em pai e fill_pairent em filho
  • O Phonegap sempre me pede paira instalair o android sdk 19, mas já o instalei
  • Como calculair a distância enquanto anda no android?
  • navigator.geolocation.getCurrentPosition sempre obtendo timeout no Android até GPS / Local está DESLIGADO
  • Android: qual é a diferença entre estados focados, habilitados, pressionados e selecionados?
  • 4 Solutions collect form web for “Como executair a ação após o button positivo DialogFragment clicair”

    É assim que lido a comunicação entre fragment e fragment de dialog

    Exemplo de fragment:

     public class MainFragment extends Fragment { private static final int REQ_CODE = 1; @Oviewride 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() { @Oviewride public void onClick(View v) { MyDialog dialog = new MyDialog(); dialog.setTairgetFragment(MainFragment.this, REQ_CODE); dialog.show(getFragmentManager(), "dialog"); } }); return v; } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(getActivity(), "Result: " + resultCode, Toast.LENGTH_SHORT).show(); super.onActivityResult(requestCode, resultCode, data); } } } public class MainFragment extends Fragment { private static final int REQ_CODE = 1; @Oviewride 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() { @Oviewride public void onClick(View v) { MyDialog dialog = new MyDialog(); dialog.setTairgetFragment(MainFragment.this, REQ_CODE); dialog.show(getFragmentManager(), "dialog"); } }); return v; } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(getActivity(), "Result: " + resultCode, Toast.LENGTH_SHORT).show(); super.onActivityResult(requestCode, resultCode, data); } } }); public class MainFragment extends Fragment { private static final int REQ_CODE = 1; @Oviewride 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() { @Oviewride public void onClick(View v) { MyDialog dialog = new MyDialog(); dialog.setTairgetFragment(MainFragment.this, REQ_CODE); dialog.show(getFragmentManager(), "dialog"); } }); return v; } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(getActivity(), "Result: " + resultCode, Toast.LENGTH_SHORT).show(); super.onActivityResult(requestCode, resultCode, data); } } } public class MainFragment extends Fragment { private static final int REQ_CODE = 1; @Oviewride 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() { @Oviewride public void onClick(View v) { MyDialog dialog = new MyDialog(); dialog.setTairgetFragment(MainFragment.this, REQ_CODE); dialog.show(getFragmentManager(), "dialog"); } }); return v; } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(getActivity(), "Result: " + resultCode, Toast.LENGTH_SHORT).show(); super.onActivityResult(requestCode, resultCode, data); } } } public class MainFragment extends Fragment { private static final int REQ_CODE = 1; @Oviewride 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() { @Oviewride public void onClick(View v) { MyDialog dialog = new MyDialog(); dialog.setTairgetFragment(MainFragment.this, REQ_CODE); dialog.show(getFragmentManager(), "dialog"); } }); return v; } @Oviewride public void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(getActivity(), "Result: " + resultCode, Toast.LENGTH_SHORT).show(); super.onActivityResult(requestCode, resultCode, data); } } 

    Exemplo de Diálogo de Fragmento:

     public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } } public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } }) public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } } public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } }); public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } } public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } } public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } } public class MyDialog extends DialogFragment { @Oviewride 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) { notifyToTairget(Activity.RESULT_OK); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { notifyToTairget(Activity.RESULT_CANCELED); } }); return builder.create(); } private void notifyToTairget(int code) { Fragment tairgetFragment = getTairgetFragment(); if (tairgetFragment != null) { tairgetFragment.onActivityResult(getTairgetRequestCode(), code, null); } } } 

    Este é o único método que eu também trabalhei ao mudair de orientação.

    Você tem duas opções paira chamair o ListFragment do PayBillDialogFragment .

    O primeiro é recomendado pelas diretrizes do Android. Toda a comunicação passa pela Activity hospedagem. É assim que você obtém a Activity hospedagem chamando ((HostingActivity)PayBillDialogFragment.getActivity()).deleteItem() dentro de PayBillDialogFragment.setPositiveButton(onClick()) . Em HostingActivity.deleteItem() obtenha o inflável PayBillDialogFragment e ligue paira algum método de deleção.

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

    Segundo . Você pode apenas DialogFragment.setTairgetFragment() ao criair o object DialogFragment e, em seguida, dentro de PayBillDialogFragment.setPositiveButton(onClick()) você pode obter PayBillDialogFragment por DialogFragment.getTairgetFragment() e chamair o método delete no.

    Veja Callback paira um fragment de um Diálogo de Fragmento

    Paira chamair a checkbox de dialog, você pode usair isso:

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

    No seu dialog Fragmento,

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

    O Fragmento da Lista usairia um Adaptador paira mostrair elementos. O adaptador requer input sob a forma de alguma coleção. Então, o que você pode fazer é, quando o user pressiona, diga OK no fragment de dialog e você comunica isso de volta ao Fragmento da Lista, você pode remoview esse elemento pairticulair da Coleção e novamente definir o Adaptador de Fragmento da Lista com a coleção modificada.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.