I want to pass the text from Maine to Fragment

Asked 2 years ago, Updated 2 years ago, 142 views

I'm a beginner at coding. I made a tap layout using an adapter When I click the button on click, I want to hand over each character in Edittext to the tab page, but I don't know how.

I found a way and tried it.

 1. If you use the bundle and use the argument, the text won't go over

    2.public Fragment findFragmentByPosition(int position) {
    return getSupportFragmentManager().findFragmentByTag(
    "android:switcher:" + viewPager.getId() + ":"
    + + ((FragmentAdapter) viewPager.getAdapter()).getItemId(position));
    } I've used the part (maybe I wrote it correctly) and if I write the text and press the button, the app turns off

I've been googling, so I don't know I've been holding onto that for 2 weeks Please answer meㅜㅜ Oh, and the onclick is missing from the main. Please think that there is an onclick

MAIN

ADAPTER

Fragment

layout

fragment activity adapter

2022-09-22 10:39

1 Answers

bundle is usually used to pass values in the FragmentTransaction process.

A typical method of finding a fragment is

You are currently using findFragmentByTag() and I can't see the part where you added the tag to the fragment with the code you posted, and it seems that you are putting the wrong value in the parameter of the method. Therefore, it seems that the app was forced to shut down due to an error log that the fragment could not be found.

Please refer to a simple example in which an id is assigned to a fragment and findFragmentById is used to deliver events between Activity <-> Fragments (It's written in Cotlin))

MainActivity.kt

class MainActivity: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn.setOnClickListener { dispatchEventToFragment() }
    }

    private fun dispatchEventToFragment() {
        val fragment = supportFragmentManager.findFragmentById(R.id.testFragment) as TestFragment
        fragment.updateValue()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".MainActivity">

    <android.support.constraint.Guideline
        android:id="@+id/tGL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".03" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hit Button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/tGL" />

    <fragment
        android:id="@+id/testFragment"
        android:name="com.pistolcaffe.hashcode.TestFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn" />

</android.support.constraint.ConstraintLayout>

TestFragment.kt

classTestFragment:Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_test, container, false)
    }

    fun updateValue() {
        value.text = Random().nextInt(1000).toString()
    }
}

fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_dark"
    tools:context=".TestFragment">

    <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="22sp"
        android:textStyle="bold" />

</FrameLayout>

In addition to this method, you can use a custom listener, and there are various methods, so please take your time.


2022-09-22 10:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.