How to get returned data from user dialog in Android?

I have the first action with one button and one edittextbox. When I click this button, I need to transfer the list of data from the first action to the second.

The second action displays this as a list. If the user selects one from the list, this specific data should return to the called action (first action) and should be displayed in the EditText field. How could I do this?

My first activity:

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

Secondly:

 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(); } } 
+4
source share
4 answers

You may have a custom interface callback like this.

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

Declare this in CustomizeDialog1 as a member of the class and make setter & getter for it. Then in 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 CustomizeDialog1 CustomizeDialog1 when the user clicks OK .

 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(); } } 
+14
source

There is a native AlertDialog that allows you to do what you are talking about.

see link AlertDialog .

+1
source
  1. Make customizeDialog variable inside First
  2. Make First implements OnDismissListener
  3. Use public void onDismiss(DialogInterface dialog) to check user input in a dialog / invoke a new action


     public class First extends Activity implements OnDismissListener { CustomizeDialog1 customizeDialog; public void onDismiss(DialogInterface dialog) { //USE customizeDialog //for example get selected value } 

Remember to edit the CustomizeDialog1 class and add recipients to be able to find out what the user has selected inside onDismiss .

0
source

Given your problem, how to transfer data between actions, and you can do this through your alertdialog. To transfer data from one activity to another, you can do this through intent as

 Intent intent = new Intent(context, ActivitytoStart.class); intent.putExtra(key, value); startActivity(intent); 

In other activities, you can get this data through:

 getIntent().getExtras().get(key); 

for your edittext to return data you can use

 startActivityForResult(intent, 0); 

and set the value in another activity as follows:

 private void setData(){ Bundle conData = new Bundle(); conData.putString(key, value); Intent intent = new Intent(); intent.putExtras(conData); setResult(RESULT_OK, intent); } 

and receive this data in their activity:

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK){ //Fetch data as above thru Intent(data) and set the value to your edittext } } 
0
source

All Articles