How to receive returned data from custom Dialog in 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 开发者_Python百科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();
}
}
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();
}
}
There are native AlertDialog
that let you do what you are talking about.
see the AlertDialog reference.
Credit to @Adil Soomro
I will share my full code for this for beginners.
- Create Interface for sharing purposes. ==> Listener.java
- Declare the Listener as your class member and generate setter and getter for this. ==> Dialog.java
When you create a Dialog Object, Please set Listener ==> MainActicity.java
**customDialog.setListener(new Listener() { public void onReturnValue(String yourInput) { Lod.d("MainActivity","you keyed in : "+yourInput); } });**
MainActivity
public class MainActivity extends AppCompatActivity {
PieChart pieChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
...
Button button = findViewById(R.id.recordButton);
final TextView main_label = findViewById(R.id.main_label);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog customDialog = new Dialog(MainActivity.this);
customDialog.setListener(new Listener() {
public void onReturnValue(String yourInput) {
Lod.d("MainActivity","you keyed in : "+yourInput);
}
});
customDialog.showDialog(main_label);
}
});
}
MainActivity XML
..
<TextView //place that you want to locate your dialog
android:id="@+id/main_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Label"
android:textSize="20dp"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
...
<Button
android:id="@+id/recordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:text="Record" />
..
Dialog
public class Dialog extends AppCompatActivity {
private Context context;
private Listener listener;
public Dialog() {
}
public Dialog(Context context) {
this.context = context;
}
public void showDialog(final TextView main_label) {
final Dialog dlg = new Dialog(context);
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
dlg.setContentView(R.layout.activity_record_dialog);
dlg.show();
final EditText readPage = dlg.findViewById(R.id.readPage);
final Button okButton = dlg.findViewById(R.id.okButton);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
main_label.setText(readPage.getText().toString());
Toast.makeText(context, "\"" + readPage.getText().toString() + "\" ", Toast.LENGTH_SHORT).show();
listener.onReturnValue(readPage.getText().toString());
dlg.dismiss();
}
});
...
public Listener getListener() {
return listener;
}
public void setListener(Listener listener) {
this.listener = listener;
}
Listener
...
public interface Listener {
public void onReturnValue(String input);
}
Considering your problem how to pass data between activity and you can do this thing thru your alertdialog too. To pass data from one activity to another you can do this thru intent as
Intent intent = new Intent(context, ActivitytoStart.class);
intent.putExtra(key, value);
startActivity(intent);
On other activity you can get that data thru:
getIntent().getExtras().get(key);
for your edittext to get back data you can use
startActivityForResult(intent, 0);
and set the value in other activity like this:
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 your calling activity as:
@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
}
}
- Make customizeDialog a variable inside First
- Make First implement OnDismissListener
Use
public void onDismiss(DialogInterface dialog)
to check for user input in dialog/call new activity
public class First extends Activity implements OnDismissListener { CustomizeDialog1 customizeDialog; public void onDismiss(DialogInterface dialog) { //USE customizeDialog //for example get selected value }
Don't forget to edit CustomizeDialog1
class and add getters to be able to know what the user selected inside onDismiss
.
精彩评论