A
A
Anton @ Lyalin2017-10-17 11:00:35
Android
Anton @ Lyalin, 2017-10-17 11:00:35

How to pass data to DialogFragment???

How to pass data from ListView to TextView which is in DialogFragment?
But three arrays of data are passed to the ListView and each of them needs to be passed to one of the TextView.

public class DialogFragment extends android.support.v4.app.DialogFragment{

    TextView tvF_Name, tvL_Name, tvB_Day;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        tvF_Name = (TextView) getActivity().findViewById(R.id.tvF_Name);
        tvL_Name = (TextView) getActivity().findViewById(R.id.tvL_Name);
        tvB_Day = (TextView) getActivity().findViewById(R.id.tvB_Day);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.dialog_fragment, null);
        
        builder.setView(view).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        })
                .setCancelable(false);


        return builder.create();


    }
}

final ListAdapter listAdapter = new SimpleAdapter(activity.getApplicationContext(),
                arrayList,
                R.layout.simple_list_item,
                from,
                to);

        listView.setAdapter(listAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DialogFragment dialogFragment = new DialogFragment();

                Bundle bundle = new Bundle();

                String f_name = listAdapter.getItem(position).toString();

                bundle.putString("name", f_name);

                dialogFragment.setArguments(bundle);
                dialogFragment.show(activity.getSupportFragmentManager(), "Dialog");
            }
        });

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tvF_Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tvL_Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tvB_Day"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp" />
</LinearLayout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Myvrenik, 2017-10-17
@gim0

You are already submitting data, specifically name : All you need to do is get the value in Fragment#onViewCreated and use it:

Bundle args = getArguments();
if (args != null) {
    String name = args.getString("name");
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question