TextView.setView () ne pas le texte de mise à jour

L4c0573:

A partir d' un fragment que je commence un activty, qui ouvre un nouveau fragment. Je passe également une valeur de chaîne à partir du premier fragment ( AllDishes) par rapport à l'activité au dernier fragment ( DishDetailView) par l' intermédiaire d' intention. Le fragment ( DishDetailView) est défini comme contexte à une mise en page ( fragment_dishdetailview), mais lorsque je tente de définir le texte qui a été adopté, la vue ne reçoit pas mis à jour.

Quand je déboguer mon application, le paramètre de chaîne ( "Test") semble être passé et aussi le texte du TextView namesemble être correctement défini, comme vous pouvez le voir dans la capture d' écran ci - dessous, mais en quelque sorte l'interface utilisateur ( fragment_dishdetailviewne reçoit pas mis à jour), d'une manière Il suffit de vider.

Je ne suis pas sûr que la mise à jour de l'interface utilisateur ou de la façon de passer la valeur d'un fragment à l' autre est le problème. Mon objectif est d'ouvrir le fragment ( DishDetailView) à partir de fragment ( AllDishes) et passer une chaîne entre le premier et le second fragment. Donc , en fait je ne pas besoin de l'activité , mais sans que je ne pouvais pas passer la chaîne.

entrez la description d'image ici

AllDishes (fragment)

@Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Dish dish = ca.getItem(position);

                Intent intent = new Intent(rootView.getContext(), DishDetailsViewActivity.class);
                intent.putExtra("nameToPass" , dish.name);
                startActivity(intent);
            }

DishDetailView (fragment)

public class DishDetailView extends Fragment {

    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        View rootView = inflater.inflate(R.layout.fragment_dishdetailview, container, false);

        String tempHolder = getActivity().getIntent().getStringExtra("nameToPass");

        TextView name = rootView.findViewById(R.id.textview_view_dishName);

        name.setText(tempHolder);

        rootView.invalidate();

        return inflater.inflate(R.layout.fragment_dishdetailview, container, false);
    }
}

DishDetailsViewActivity (activité)

public class DishDetailsViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dish_details_view);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

fragment_dishdetailview.xml (mise en page fragment)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/screen_background"
    tools:context=".fragments.DishDetailView">

    <TextView
        android:id="@+id/textview_view_dishName"
        android:layout_width="300dp"
        android:layout_height="27dp"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/headercolor"
        android:textSize="@dimen/header_fontsize"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

** content_dish_details_view.xml (mise en page d'activité) **

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph3" />
</androidx.constraintlayout.widget.ConstraintLayout>

nav-graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph3"
    app:startDestination="@id/fragment_dishdetailview">

    <fragment
        android:id="@+id/fragment_dishdetailview"
        android:name="com.example.nutritracker.fragments.DishDetailView"
        android:label="DishDetailView"
        tools:layout="@layout/fragment_dishdetailview">
    </fragment>
</navigation>
Luca:

Je pense que vous devriez retourner votre rootViewsur la onCreateViewméthode DishDetailView.

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=373251&siteId=1
conseillé
Classement