passing argument to DialogFragment
I need to pass some variables to DialogFragment
, so I can perform an action. Eclipse suggests that I should use
Fragment#setArguments(Bundle)
But I don't know how to use this function. How can I use it to pass variables to my dialog?
android android-dialogfragment
add a comment |
I need to pass some variables to DialogFragment
, so I can perform an action. Eclipse suggests that I should use
Fragment#setArguments(Bundle)
But I don't know how to use this function. How can I use it to pass variables to my dialog?
android android-dialogfragment
Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…
– IgorGanapolsky
Feb 12 '14 at 23:12
add a comment |
I need to pass some variables to DialogFragment
, so I can perform an action. Eclipse suggests that I should use
Fragment#setArguments(Bundle)
But I don't know how to use this function. How can I use it to pass variables to my dialog?
android android-dialogfragment
I need to pass some variables to DialogFragment
, so I can perform an action. Eclipse suggests that I should use
Fragment#setArguments(Bundle)
But I don't know how to use this function. How can I use it to pass variables to my dialog?
android android-dialogfragment
android android-dialogfragment
edited Jul 19 '15 at 12:58
yochannah
2,27553147
2,27553147
asked Mar 17 '13 at 9:20
giozhgiozh
3,6052076137
3,6052076137
Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…
– IgorGanapolsky
Feb 12 '14 at 23:12
add a comment |
Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…
– IgorGanapolsky
Feb 12 '14 at 23:12
Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…
– IgorGanapolsky
Feb 12 '14 at 23:12
Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…
– IgorGanapolsky
Feb 12 '14 at 23:12
add a comment |
6 Answers
6
active
oldest
votes
Using newInstance
public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
And get the Args like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
...
}
See the full example here
http://developer.android.com/reference/android/app/DialogFragment.html
Can you just set private variables on MyDialogFragment instead of using bundle?
– SIr Codealot
Feb 19 '15 at 0:56
8
@SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.
– inmyth
Mar 16 '15 at 4:49
1
For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…
– HondaGuy
May 31 '17 at 20:05
Took me a minute to notice that the savedInstanceState is not used.
– Odys
May 31 '18 at 11:32
add a comment |
I used to send some values from my listview
How to send
mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Favorite clickedObj = (Favorite) parent.getItemAtPosition(position);
Bundle args = new Bundle();
args.putString("tar_name", clickedObj.getNameTarife());
args.putString("fav_name", clickedObj.getName());
FragmentManager fragmentManager = getSupportFragmentManager();
TarifeDetayPopup userPopUp = new TarifeDetayPopup();
userPopUp.setArguments(args);
userPopUp.show(fragmentManager, "sam");
return false;
}
});
How to receive inside onCreate() method of DialogFragment
Bundle mArgs = getArguments();
String nameTrife = mArgs.getString("tar_name");
String nameFav = mArgs.getString("fav_name");
String name = "";
// Kotlin upload
val fm = supportFragmentManager
val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
var args: Bundle? = null
args?.putString("title", model.title);
dialogFragment.setArguments(args)
dialogFragment.show(fm, "Sample Fragment")
// receive
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (getArguments() != null) {
val mArgs = arguments
var myDay= mArgs.getString("title")
}
}
1
The Better answer!
– user55924
Mar 11 '18 at 14:40
add a comment |
as a general way of working with Fragments, as JafarKhQ noted, you should not pass the params in the constructor but with a Bundle
.
the built-in method for that in the Fragment
class is setArguments(Bundle)
and getArguments()
.
basically, what you do is set up a bundle with all your Parcelable
items and send them on.
in turn, your Fragment will get those items in it's onCreate
and do it's magic to them.
the way shown in the DialogFragment
link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.
add a comment |
So there is two ways to pass values from fragment/activity to dialog fragment:-
Create dialog fragment object with make setter method and pass value/argument.
Pass value/argument through bundle.
Method 1:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
dialog.setValue(header, body);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
public void setValue(String header, String body) {
this.header = header;
this.body = body;
}
// use above variable into your dialog fragment
}
Note:- This is not best way to do
Method 2:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
Bundle bundle = new Bundle();
bundle.putString("header", "Header");
bundle.putString("body", "Body");
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
header = getArguments().getString("header","");
body = getArguments().getString("body","");
}
}
// use above variable into your dialog fragment
}
Note:- This is the best way to do.
You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.
– Jeffrey
Aug 11 '18 at 7:30
add a comment |
In my case, none of the code above with bundle-operate
works; Here is my decision (I don't know if it is proper code or not, but it works in my case):
public class DialogMessageType extends DialogFragment {
private static String bodyText;
public static DialogMessageType addSomeString(String temp){
DialogMessageType f = new DialogMessageType();
bodyText = temp;
return f;
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String choiseArray = {"sms", "email"};
String title = "Send text via:";
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title).setItems(choiseArray, itemClickListener);
builder.setCancelable(true);
return builder.create();
}
DialogInterface.OnClickListener itemClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
prepareToSendCoordsViaSMS(bodyText);
dialog.dismiss();
break;
case 1:
prepareToSendCoordsViaEmail(bodyText);
dialog.dismiss();
break;
default:
break;
}
}
};
[...]
}
public class SendObjectActivity extends FragmentActivity {
[...]
DialogMessageType dialogMessageType = DialogMessageType.addSomeString(stringToSend);
dialogMessageType.show(getSupportFragmentManager(),"dialogMessageType");
[...]
}
1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.
– JHH
Oct 26 '16 at 10:52
add a comment |
Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:
so you have to create a companion objet to create new newInstance function
you can set the paremter of the function whatever you want.
using
val args = Bundle()
you can set your args.
You can now use args.putSomthing
to add you args which u give as a prameter in your newInstance function.
putString(key:String,str:String)
to add string for example and so on
Now to get the argument you can use
arguments.getSomthing(Key:String)
=> like arguments.getString("1")
here is a full example
class IntervModifFragment : DialogFragment(), ModContract.View
{
companion object {
fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
val fragment =IntervModifFragment()
val args = Bundle()
args.putString( "1",plom)
args.putString("2",type)
args.putInt("3",position)
fragment.arguments = args
return fragment
}
}
...
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fillSpinerPlom(view,arguments.getString("1"))
fillSpinerType(view, arguments.getString("2"))
confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int) {
val datep= DateT(year,monthOfYear,dayOfMonth)
updateDateInView(datep.date)
}
}
}
...
}
Now how to create your dialog you can do somthing like this in another class
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
like this for example
class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
{
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
...
holder.btn_update!!.setOnClickListener {
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
dialog.show(ft, ContentValues.TAG)
}
...
}
..
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15459209%2fpassing-argument-to-dialogfragment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using newInstance
public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
And get the Args like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
...
}
See the full example here
http://developer.android.com/reference/android/app/DialogFragment.html
Can you just set private variables on MyDialogFragment instead of using bundle?
– SIr Codealot
Feb 19 '15 at 0:56
8
@SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.
– inmyth
Mar 16 '15 at 4:49
1
For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…
– HondaGuy
May 31 '17 at 20:05
Took me a minute to notice that the savedInstanceState is not used.
– Odys
May 31 '18 at 11:32
add a comment |
Using newInstance
public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
And get the Args like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
...
}
See the full example here
http://developer.android.com/reference/android/app/DialogFragment.html
Can you just set private variables on MyDialogFragment instead of using bundle?
– SIr Codealot
Feb 19 '15 at 0:56
8
@SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.
– inmyth
Mar 16 '15 at 4:49
1
For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…
– HondaGuy
May 31 '17 at 20:05
Took me a minute to notice that the savedInstanceState is not used.
– Odys
May 31 '18 at 11:32
add a comment |
Using newInstance
public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
And get the Args like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
...
}
See the full example here
http://developer.android.com/reference/android/app/DialogFragment.html
Using newInstance
public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
And get the Args like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
...
}
See the full example here
http://developer.android.com/reference/android/app/DialogFragment.html
edited Oct 7 '15 at 19:28
Adam Varhegyi
5,5322891166
5,5322891166
answered Mar 17 '13 at 9:26
JafarKhQJafarKhQ
7,53122940
7,53122940
Can you just set private variables on MyDialogFragment instead of using bundle?
– SIr Codealot
Feb 19 '15 at 0:56
8
@SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.
– inmyth
Mar 16 '15 at 4:49
1
For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…
– HondaGuy
May 31 '17 at 20:05
Took me a minute to notice that the savedInstanceState is not used.
– Odys
May 31 '18 at 11:32
add a comment |
Can you just set private variables on MyDialogFragment instead of using bundle?
– SIr Codealot
Feb 19 '15 at 0:56
8
@SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.
– inmyth
Mar 16 '15 at 4:49
1
For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…
– HondaGuy
May 31 '17 at 20:05
Took me a minute to notice that the savedInstanceState is not used.
– Odys
May 31 '18 at 11:32
Can you just set private variables on MyDialogFragment instead of using bundle?
– SIr Codealot
Feb 19 '15 at 0:56
Can you just set private variables on MyDialogFragment instead of using bundle?
– SIr Codealot
Feb 19 '15 at 0:56
8
8
@SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.
– inmyth
Mar 16 '15 at 4:49
@SIrCodealot the effect would be the same as setting variables on Activity or Fragment. If you face something that destroys and recreates the DialogDragment like rotation change, you will lose all variables.
– inmyth
Mar 16 '15 at 4:49
1
1
For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…
– HondaGuy
May 31 '17 at 20:05
For all those wondering why an overloaded constructor isn't used in this case, see another discussion on the topic that is highly instructive: stackoverflow.com/questions/14011808/…
– HondaGuy
May 31 '17 at 20:05
Took me a minute to notice that the savedInstanceState is not used.
– Odys
May 31 '18 at 11:32
Took me a minute to notice that the savedInstanceState is not used.
– Odys
May 31 '18 at 11:32
add a comment |
I used to send some values from my listview
How to send
mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Favorite clickedObj = (Favorite) parent.getItemAtPosition(position);
Bundle args = new Bundle();
args.putString("tar_name", clickedObj.getNameTarife());
args.putString("fav_name", clickedObj.getName());
FragmentManager fragmentManager = getSupportFragmentManager();
TarifeDetayPopup userPopUp = new TarifeDetayPopup();
userPopUp.setArguments(args);
userPopUp.show(fragmentManager, "sam");
return false;
}
});
How to receive inside onCreate() method of DialogFragment
Bundle mArgs = getArguments();
String nameTrife = mArgs.getString("tar_name");
String nameFav = mArgs.getString("fav_name");
String name = "";
// Kotlin upload
val fm = supportFragmentManager
val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
var args: Bundle? = null
args?.putString("title", model.title);
dialogFragment.setArguments(args)
dialogFragment.show(fm, "Sample Fragment")
// receive
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (getArguments() != null) {
val mArgs = arguments
var myDay= mArgs.getString("title")
}
}
1
The Better answer!
– user55924
Mar 11 '18 at 14:40
add a comment |
I used to send some values from my listview
How to send
mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Favorite clickedObj = (Favorite) parent.getItemAtPosition(position);
Bundle args = new Bundle();
args.putString("tar_name", clickedObj.getNameTarife());
args.putString("fav_name", clickedObj.getName());
FragmentManager fragmentManager = getSupportFragmentManager();
TarifeDetayPopup userPopUp = new TarifeDetayPopup();
userPopUp.setArguments(args);
userPopUp.show(fragmentManager, "sam");
return false;
}
});
How to receive inside onCreate() method of DialogFragment
Bundle mArgs = getArguments();
String nameTrife = mArgs.getString("tar_name");
String nameFav = mArgs.getString("fav_name");
String name = "";
// Kotlin upload
val fm = supportFragmentManager
val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
var args: Bundle? = null
args?.putString("title", model.title);
dialogFragment.setArguments(args)
dialogFragment.show(fm, "Sample Fragment")
// receive
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (getArguments() != null) {
val mArgs = arguments
var myDay= mArgs.getString("title")
}
}
1
The Better answer!
– user55924
Mar 11 '18 at 14:40
add a comment |
I used to send some values from my listview
How to send
mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Favorite clickedObj = (Favorite) parent.getItemAtPosition(position);
Bundle args = new Bundle();
args.putString("tar_name", clickedObj.getNameTarife());
args.putString("fav_name", clickedObj.getName());
FragmentManager fragmentManager = getSupportFragmentManager();
TarifeDetayPopup userPopUp = new TarifeDetayPopup();
userPopUp.setArguments(args);
userPopUp.show(fragmentManager, "sam");
return false;
}
});
How to receive inside onCreate() method of DialogFragment
Bundle mArgs = getArguments();
String nameTrife = mArgs.getString("tar_name");
String nameFav = mArgs.getString("fav_name");
String name = "";
// Kotlin upload
val fm = supportFragmentManager
val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
var args: Bundle? = null
args?.putString("title", model.title);
dialogFragment.setArguments(args)
dialogFragment.show(fm, "Sample Fragment")
// receive
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (getArguments() != null) {
val mArgs = arguments
var myDay= mArgs.getString("title")
}
}
I used to send some values from my listview
How to send
mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Favorite clickedObj = (Favorite) parent.getItemAtPosition(position);
Bundle args = new Bundle();
args.putString("tar_name", clickedObj.getNameTarife());
args.putString("fav_name", clickedObj.getName());
FragmentManager fragmentManager = getSupportFragmentManager();
TarifeDetayPopup userPopUp = new TarifeDetayPopup();
userPopUp.setArguments(args);
userPopUp.show(fragmentManager, "sam");
return false;
}
});
How to receive inside onCreate() method of DialogFragment
Bundle mArgs = getArguments();
String nameTrife = mArgs.getString("tar_name");
String nameFav = mArgs.getString("fav_name");
String name = "";
// Kotlin upload
val fm = supportFragmentManager
val dialogFragment = AddProgFargmentDialog() // my custom FargmentDialog
var args: Bundle? = null
args?.putString("title", model.title);
dialogFragment.setArguments(args)
dialogFragment.show(fm, "Sample Fragment")
// receive
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (getArguments() != null) {
val mArgs = arguments
var myDay= mArgs.getString("title")
}
}
edited Oct 20 '18 at 7:56
answered Jun 19 '17 at 10:21
SamSam
2,36342331
2,36342331
1
The Better answer!
– user55924
Mar 11 '18 at 14:40
add a comment |
1
The Better answer!
– user55924
Mar 11 '18 at 14:40
1
1
The Better answer!
– user55924
Mar 11 '18 at 14:40
The Better answer!
– user55924
Mar 11 '18 at 14:40
add a comment |
as a general way of working with Fragments, as JafarKhQ noted, you should not pass the params in the constructor but with a Bundle
.
the built-in method for that in the Fragment
class is setArguments(Bundle)
and getArguments()
.
basically, what you do is set up a bundle with all your Parcelable
items and send them on.
in turn, your Fragment will get those items in it's onCreate
and do it's magic to them.
the way shown in the DialogFragment
link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.
add a comment |
as a general way of working with Fragments, as JafarKhQ noted, you should not pass the params in the constructor but with a Bundle
.
the built-in method for that in the Fragment
class is setArguments(Bundle)
and getArguments()
.
basically, what you do is set up a bundle with all your Parcelable
items and send them on.
in turn, your Fragment will get those items in it's onCreate
and do it's magic to them.
the way shown in the DialogFragment
link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.
add a comment |
as a general way of working with Fragments, as JafarKhQ noted, you should not pass the params in the constructor but with a Bundle
.
the built-in method for that in the Fragment
class is setArguments(Bundle)
and getArguments()
.
basically, what you do is set up a bundle with all your Parcelable
items and send them on.
in turn, your Fragment will get those items in it's onCreate
and do it's magic to them.
the way shown in the DialogFragment
link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.
as a general way of working with Fragments, as JafarKhQ noted, you should not pass the params in the constructor but with a Bundle
.
the built-in method for that in the Fragment
class is setArguments(Bundle)
and getArguments()
.
basically, what you do is set up a bundle with all your Parcelable
items and send them on.
in turn, your Fragment will get those items in it's onCreate
and do it's magic to them.
the way shown in the DialogFragment
link was one way of doing this in a multi appearing fragment with one specific type of data and works fine most of the time, but you can also do this manually.
answered Mar 17 '13 at 9:45
thepooshthepoosh
9,4121359119
9,4121359119
add a comment |
add a comment |
So there is two ways to pass values from fragment/activity to dialog fragment:-
Create dialog fragment object with make setter method and pass value/argument.
Pass value/argument through bundle.
Method 1:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
dialog.setValue(header, body);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
public void setValue(String header, String body) {
this.header = header;
this.body = body;
}
// use above variable into your dialog fragment
}
Note:- This is not best way to do
Method 2:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
Bundle bundle = new Bundle();
bundle.putString("header", "Header");
bundle.putString("body", "Body");
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
header = getArguments().getString("header","");
body = getArguments().getString("body","");
}
}
// use above variable into your dialog fragment
}
Note:- This is the best way to do.
You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.
– Jeffrey
Aug 11 '18 at 7:30
add a comment |
So there is two ways to pass values from fragment/activity to dialog fragment:-
Create dialog fragment object with make setter method and pass value/argument.
Pass value/argument through bundle.
Method 1:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
dialog.setValue(header, body);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
public void setValue(String header, String body) {
this.header = header;
this.body = body;
}
// use above variable into your dialog fragment
}
Note:- This is not best way to do
Method 2:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
Bundle bundle = new Bundle();
bundle.putString("header", "Header");
bundle.putString("body", "Body");
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
header = getArguments().getString("header","");
body = getArguments().getString("body","");
}
}
// use above variable into your dialog fragment
}
Note:- This is the best way to do.
You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.
– Jeffrey
Aug 11 '18 at 7:30
add a comment |
So there is two ways to pass values from fragment/activity to dialog fragment:-
Create dialog fragment object with make setter method and pass value/argument.
Pass value/argument through bundle.
Method 1:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
dialog.setValue(header, body);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
public void setValue(String header, String body) {
this.header = header;
this.body = body;
}
// use above variable into your dialog fragment
}
Note:- This is not best way to do
Method 2:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
Bundle bundle = new Bundle();
bundle.putString("header", "Header");
bundle.putString("body", "Body");
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
header = getArguments().getString("header","");
body = getArguments().getString("body","");
}
}
// use above variable into your dialog fragment
}
Note:- This is the best way to do.
So there is two ways to pass values from fragment/activity to dialog fragment:-
Create dialog fragment object with make setter method and pass value/argument.
Pass value/argument through bundle.
Method 1:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
dialog.setValue(header, body);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
public void setValue(String header, String body) {
this.header = header;
this.body = body;
}
// use above variable into your dialog fragment
}
Note:- This is not best way to do
Method 2:
// Fragment or Activity
@Override
public void onClick(View v) {
DialogFragmentWithSetter dialog = new DialogFragmentWithSetter();
Bundle bundle = new Bundle();
bundle.putString("header", "Header");
bundle.putString("body", "Body");
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "DialogFragmentWithSetter");
}
// your dialog fragment
public class MyDialogFragment extends DialogFragment {
String header;
String body;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
header = getArguments().getString("header","");
body = getArguments().getString("body","");
}
}
// use above variable into your dialog fragment
}
Note:- This is the best way to do.
answered Feb 9 '18 at 10:00
dugguduggu
29.7k99099
29.7k99099
You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.
– Jeffrey
Aug 11 '18 at 7:30
add a comment |
You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.
– Jeffrey
Aug 11 '18 at 7:30
You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.
– Jeffrey
Aug 11 '18 at 7:30
You can even use the Gson library to pass Objects or ArrayList<Objects> as Strings in the Bundle.
– Jeffrey
Aug 11 '18 at 7:30
add a comment |
In my case, none of the code above with bundle-operate
works; Here is my decision (I don't know if it is proper code or not, but it works in my case):
public class DialogMessageType extends DialogFragment {
private static String bodyText;
public static DialogMessageType addSomeString(String temp){
DialogMessageType f = new DialogMessageType();
bodyText = temp;
return f;
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String choiseArray = {"sms", "email"};
String title = "Send text via:";
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title).setItems(choiseArray, itemClickListener);
builder.setCancelable(true);
return builder.create();
}
DialogInterface.OnClickListener itemClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
prepareToSendCoordsViaSMS(bodyText);
dialog.dismiss();
break;
case 1:
prepareToSendCoordsViaEmail(bodyText);
dialog.dismiss();
break;
default:
break;
}
}
};
[...]
}
public class SendObjectActivity extends FragmentActivity {
[...]
DialogMessageType dialogMessageType = DialogMessageType.addSomeString(stringToSend);
dialogMessageType.show(getSupportFragmentManager(),"dialogMessageType");
[...]
}
1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.
– JHH
Oct 26 '16 at 10:52
add a comment |
In my case, none of the code above with bundle-operate
works; Here is my decision (I don't know if it is proper code or not, but it works in my case):
public class DialogMessageType extends DialogFragment {
private static String bodyText;
public static DialogMessageType addSomeString(String temp){
DialogMessageType f = new DialogMessageType();
bodyText = temp;
return f;
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String choiseArray = {"sms", "email"};
String title = "Send text via:";
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title).setItems(choiseArray, itemClickListener);
builder.setCancelable(true);
return builder.create();
}
DialogInterface.OnClickListener itemClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
prepareToSendCoordsViaSMS(bodyText);
dialog.dismiss();
break;
case 1:
prepareToSendCoordsViaEmail(bodyText);
dialog.dismiss();
break;
default:
break;
}
}
};
[...]
}
public class SendObjectActivity extends FragmentActivity {
[...]
DialogMessageType dialogMessageType = DialogMessageType.addSomeString(stringToSend);
dialogMessageType.show(getSupportFragmentManager(),"dialogMessageType");
[...]
}
1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.
– JHH
Oct 26 '16 at 10:52
add a comment |
In my case, none of the code above with bundle-operate
works; Here is my decision (I don't know if it is proper code or not, but it works in my case):
public class DialogMessageType extends DialogFragment {
private static String bodyText;
public static DialogMessageType addSomeString(String temp){
DialogMessageType f = new DialogMessageType();
bodyText = temp;
return f;
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String choiseArray = {"sms", "email"};
String title = "Send text via:";
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title).setItems(choiseArray, itemClickListener);
builder.setCancelable(true);
return builder.create();
}
DialogInterface.OnClickListener itemClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
prepareToSendCoordsViaSMS(bodyText);
dialog.dismiss();
break;
case 1:
prepareToSendCoordsViaEmail(bodyText);
dialog.dismiss();
break;
default:
break;
}
}
};
[...]
}
public class SendObjectActivity extends FragmentActivity {
[...]
DialogMessageType dialogMessageType = DialogMessageType.addSomeString(stringToSend);
dialogMessageType.show(getSupportFragmentManager(),"dialogMessageType");
[...]
}
In my case, none of the code above with bundle-operate
works; Here is my decision (I don't know if it is proper code or not, but it works in my case):
public class DialogMessageType extends DialogFragment {
private static String bodyText;
public static DialogMessageType addSomeString(String temp){
DialogMessageType f = new DialogMessageType();
bodyText = temp;
return f;
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String choiseArray = {"sms", "email"};
String title = "Send text via:";
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title).setItems(choiseArray, itemClickListener);
builder.setCancelable(true);
return builder.create();
}
DialogInterface.OnClickListener itemClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
prepareToSendCoordsViaSMS(bodyText);
dialog.dismiss();
break;
case 1:
prepareToSendCoordsViaEmail(bodyText);
dialog.dismiss();
break;
default:
break;
}
}
};
[...]
}
public class SendObjectActivity extends FragmentActivity {
[...]
DialogMessageType dialogMessageType = DialogMessageType.addSomeString(stringToSend);
dialogMessageType.show(getSupportFragmentManager(),"dialogMessageType");
[...]
}
edited Oct 8 '15 at 17:28
admdrew
3,18241936
3,18241936
answered Oct 8 '15 at 17:04
KirillKirill
11
11
1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.
– JHH
Oct 26 '16 at 10:52
add a comment |
1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.
– JHH
Oct 26 '16 at 10:52
1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.
– JHH
Oct 26 '16 at 10:52
1) By storing the bodyText statically, you effectively make it impossible to have two instances of this class at the same time, with different body texts. There's no reason not to store it as an instance variable instead. 2) The whole purpose of sending arguments using setArguments(Bundle) is that the OS can then re-create the fragment in case it's lost in a low memory situation etc. With your solution the fragment will be re-created, and body text will be whatever the last instance of the dialog used (since it's static). The correct solution is to set the body text as a bundle parameter.
– JHH
Oct 26 '16 at 10:52
add a comment |
Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:
so you have to create a companion objet to create new newInstance function
you can set the paremter of the function whatever you want.
using
val args = Bundle()
you can set your args.
You can now use args.putSomthing
to add you args which u give as a prameter in your newInstance function.
putString(key:String,str:String)
to add string for example and so on
Now to get the argument you can use
arguments.getSomthing(Key:String)
=> like arguments.getString("1")
here is a full example
class IntervModifFragment : DialogFragment(), ModContract.View
{
companion object {
fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
val fragment =IntervModifFragment()
val args = Bundle()
args.putString( "1",plom)
args.putString("2",type)
args.putInt("3",position)
fragment.arguments = args
return fragment
}
}
...
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fillSpinerPlom(view,arguments.getString("1"))
fillSpinerType(view, arguments.getString("2"))
confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int) {
val datep= DateT(year,monthOfYear,dayOfMonth)
updateDateInView(datep.date)
}
}
}
...
}
Now how to create your dialog you can do somthing like this in another class
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
like this for example
class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
{
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
...
holder.btn_update!!.setOnClickListener {
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
dialog.show(ft, ContentValues.TAG)
}
...
}
..
}
add a comment |
Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:
so you have to create a companion objet to create new newInstance function
you can set the paremter of the function whatever you want.
using
val args = Bundle()
you can set your args.
You can now use args.putSomthing
to add you args which u give as a prameter in your newInstance function.
putString(key:String,str:String)
to add string for example and so on
Now to get the argument you can use
arguments.getSomthing(Key:String)
=> like arguments.getString("1")
here is a full example
class IntervModifFragment : DialogFragment(), ModContract.View
{
companion object {
fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
val fragment =IntervModifFragment()
val args = Bundle()
args.putString( "1",plom)
args.putString("2",type)
args.putInt("3",position)
fragment.arguments = args
return fragment
}
}
...
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fillSpinerPlom(view,arguments.getString("1"))
fillSpinerType(view, arguments.getString("2"))
confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int) {
val datep= DateT(year,monthOfYear,dayOfMonth)
updateDateInView(datep.date)
}
}
}
...
}
Now how to create your dialog you can do somthing like this in another class
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
like this for example
class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
{
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
...
holder.btn_update!!.setOnClickListener {
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
dialog.show(ft, ContentValues.TAG)
}
...
}
..
}
add a comment |
Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:
so you have to create a companion objet to create new newInstance function
you can set the paremter of the function whatever you want.
using
val args = Bundle()
you can set your args.
You can now use args.putSomthing
to add you args which u give as a prameter in your newInstance function.
putString(key:String,str:String)
to add string for example and so on
Now to get the argument you can use
arguments.getSomthing(Key:String)
=> like arguments.getString("1")
here is a full example
class IntervModifFragment : DialogFragment(), ModContract.View
{
companion object {
fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
val fragment =IntervModifFragment()
val args = Bundle()
args.putString( "1",plom)
args.putString("2",type)
args.putInt("3",position)
fragment.arguments = args
return fragment
}
}
...
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fillSpinerPlom(view,arguments.getString("1"))
fillSpinerType(view, arguments.getString("2"))
confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int) {
val datep= DateT(year,monthOfYear,dayOfMonth)
updateDateInView(datep.date)
}
}
}
...
}
Now how to create your dialog you can do somthing like this in another class
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
like this for example
class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
{
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
...
holder.btn_update!!.setOnClickListener {
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
dialog.show(ft, ContentValues.TAG)
}
...
}
..
}
Just that i want to show how to do what do said @JafarKhQ in Kotlin for those who use kotlin that might help them and save theme time too:
so you have to create a companion objet to create new newInstance function
you can set the paremter of the function whatever you want.
using
val args = Bundle()
you can set your args.
You can now use args.putSomthing
to add you args which u give as a prameter in your newInstance function.
putString(key:String,str:String)
to add string for example and so on
Now to get the argument you can use
arguments.getSomthing(Key:String)
=> like arguments.getString("1")
here is a full example
class IntervModifFragment : DialogFragment(), ModContract.View
{
companion object {
fun newInstance( plom:String,type:String,position: Int):IntervModifFragment {
val fragment =IntervModifFragment()
val args = Bundle()
args.putString( "1",plom)
args.putString("2",type)
args.putInt("3",position)
fragment.arguments = args
return fragment
}
}
...
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fillSpinerPlom(view,arguments.getString("1"))
fillSpinerType(view, arguments.getString("2"))
confirmer_virme.setOnClickListener({on_confirmClick( arguments.getInt("3"))})
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int) {
val datep= DateT(year,monthOfYear,dayOfMonth)
updateDateInView(datep.date)
}
}
}
...
}
Now how to create your dialog you can do somthing like this in another class
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
like this for example
class InterListAdapter(private val context: Context, linkedList: LinkedList<InterItem> ) : RecyclerView.Adapter<InterListAdapter.ViewHolder>()
{
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
...
holder.btn_update!!.setOnClickListener {
val dialog = IntervModifFragment.newInstance(ListInter.list[position].plom,ListInter.list[position].type,position)
val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
dialog.show(ft, ContentValues.TAG)
}
...
}
..
}
answered May 18 '18 at 15:17
DINA TAKLITDINA TAKLIT
717722
717722
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15459209%2fpassing-argument-to-dialogfragment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Look at the sample code: androidxref.com/4.4.2_r1/xref/development/samples/ApiDemos/src/…
– IgorGanapolsky
Feb 12 '14 at 23:12