问题
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?
回答1:
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
回答2:
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")
}
}
回答3:
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.
回答4:
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.
回答5:
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");
[...]
}
回答6:
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)
}
...
}
..
}
来源:https://stackoverflow.com/questions/15459209/passing-argument-to-dialogfragment