TextView.setView () nicht aktualisiert Text

L4c0573:

Von einem Fragment leite ich eine activty, die ein neues Fragment öffnet. Ich gehe auch einen String - Wert aus dem ersten Fragmente ( AllDishes) über die Aktivität auf das letzte Fragment ( DishDetailView) über Absicht. Das Fragment ( DishDetailView) als Kontext zu einem Layout (gesetzt fragment_dishdetailview), aber wenn ich versuche , den Text zu setzen, übergeben wurde, wird die Ansicht nicht aktualisiert zu werden .

Wenn ich meine Anwendung debuggen, die String - Parameter ( "Test"scheinen) übergeben werden und auch der Text der TextView namescheint richtig eingestellt werden, da Sie in der unten stehenden Screenshot sehen, aber irgendwie die UI ( fragment_dishdetailview) werden nicht aktualisiert zu werden , es ist nur leer.

Ich bin nicht sicher , ob das Update der Benutzeroberfläche oder der Weg , um den Wert von einem Fragment zu einem anderen vorbei das Problem ist. Mein Ziel ist es, das Fragment (zu öffnen DishDetailView) von Fragment ( AllDishes) und eine Zeichenfolge aus der ersten in die zweite Fragment passieren. Also eigentlich dont ich die Aktivität braucht , aber ohne ich war nicht in der Lage , den String zu übergeben.

Geben Sie hier image description

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 (Aktivität)

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 (fragment Layout)

<?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 (Aktivität Layout) **

<?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:

Ich glaube , Sie Ihre Rückkehr sollte rootViewauf der onCreateViewMethode der DishDetailView.

Ich denke du magst

Origin http://43.154.161.224:23101/article/api/json?id=373246&siteId=1
Empfohlen
Rangfolge