Pergunta

i have no idea with how to add the dialog function into my onclick function,i want it the dialog to be summon once i click the button.

this is the oncreate method

 /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    fillData();
    boxAdapter = new ListAdapter(this, products);

    ListView lvMain = (ListView) findViewById(R.id.lvMain);
    lvMain.setAdapter(boxAdapter);


    Button btn = (Button) findViewById(R.id.insert);

    OnClickListener listener = new OnClickListener() {          
        @Override
        public void onClick(View v) {                               


        }
    };

    /** Setting the event listener for the add button */
    btn.setOnClickListener(listener);       



  }

then this is my dialog function which is in the same java class

public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      // Get the layout inflater
      LayoutInflater inflater = this.getLayoutInflater();

      // Inflate and set the layout for the dialog
      // Pass null as the parent view because its going in the dialog layout
      builder.setView(inflater.inflate(R.layout.dialog, null))
      // Add action buttons
             .setPositiveButton(R.string.Insert, new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {

                 }
             })
             .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                    // DialogFragment.this.getDialog().cancel();
                 }
             });      
      return builder.create();
  }
Foi útil?

Solução

Here the answer, make Bundle as final

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    fillData();
    boxAdapter = new ListAdapter(this, products);

ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);


Button btn = (Button) findViewById(R.id.insert);

OnClickListener listener = new OnClickListener() {          
    @Override
    public void onClick(View v) {                               
            Dialog d = onCreateDialog(savedInstanceState);
            d.show();

    }
};

/** Setting the event listener for the add button */
btn.setOnClickListener(listener);       

}

Outras dicas

In your Dialog function you don't have to pass the argument Bundle savedInstanceState, then in your onClick function for your button call the function like this :

  OnClickListener listener = new OnClickListener() {          
        @Override
        public void onClick(View v) {                               
Dialog dialog = onCreateDialog(); // assuming that you will remove the argmunent
dialog.show();

        }
    };
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top