Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive returned data from custom Dialog in Android?

Tags:

android

I have first activity with one button and one edittextbox. When I click that button I need to pass list of data from first activity to second activity.

The second activity will displays that as list. If user selects one from the list, that particular data should move back to the called activity (first activity) and should be displayed on the EditText box. How could I do that?

My FirstActivity:

public class First extends Activity {

    Button click;
    EditText edit;

    ArrayList<String> site=new ArrayList<String>();
    String[] sitestr=new String[]{"monkey","donkey","Elephant","Baffalo"};


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        click=(Button)findViewById(R.id.click);
        edit=(EditText)findViewById(R.id.edit);

        click.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                  CustomizeDialog1 customizeDialog = new CustomizeDialog1(tu.this,sitestr);
                  customizeDialog.show();

            }
        });
}

Second:

public class CustomizeDialog1 extends Dialog implements OnClickListener, OnItemClickListener {
  String selected_value;
  Button okButton;
  String hi[];
//  ListView list_view;

  public CustomizeDialog1(Context context,String[] value) {
    super(context);
    hi=value;
   // Log.v("Length",""+hi.length);
    /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    /** Design the dialog in main.xml file */
    setContentView(R.layout.listviewinflate);

    ListView lst=(ListView)findViewById(R.id.list1);

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,hi);

     lst.setAdapter(adapter);
     lst.setOnItemClickListener(this);

   okButton = (Button) findViewById(R.id.cancel);
   okButton.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    /** When OK Button is clicked, dismiss the dialog */
    if (v == okButton)
      dismiss();
  }

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    selected_value=hi[arg2];
    //Log.v("selected_value", selected_value);
    Toast.makeText(getContext(), selected_value,Toast.LENGTH_SHORT).show();
}
}
like image 547
sanjay Avatar asked Dec 06 '22 20:12

sanjay


2 Answers

You can have custom callback interface like this.

public static interface MyDialogListener
{
    public void userSelectedAValue(String value);
    public void userCanceled();
}

Declare this in you CustomizeDialog1 as class member and make setter & getter for it. Then in your onClick inside Activity:

public void onClick(View v){
    CustomizeDialog1 customizeDialog = new CustomizeDialog1(tu.this,sitestr);
    customizeDialog.setMyDialogListener( new MyDialogListener()
    {
    public void userSelectedAValue(String value)
    {
        //use value
    }
    public void userCanceled()
    {
    }
    });
    customizeDialog.show();

}

and in you CustomizeDialog1 when user press OK button.

public void onClick(View v)
{
    /** When OK Button is clicked, dismiss the dialog */
    if (v == okButton)
    {
        listener.userSelectedAValue(selected_value);
        // listener is object of your MyDialogListener, which you have set from
        // Activity.
        dismiss();
    }
}
like image 176
Adil Soomro Avatar answered Jan 19 '23 07:01

Adil Soomro


There are native AlertDialog that let you do what you are talking about.

see the AlertDialog reference.

like image 30
Mario Lenci Avatar answered Jan 19 '23 08:01

Mario Lenci