Cistem
Cistem

Reputation: 133

How do i retrieve inputs from fragments i added at runtime

Click to view issues on retrieving inputs from added fragments at run time

From the image above, i could only retrieve the first input from the first fragment i added dynamically but i could not retrieve for the other ones added afterwards. Please can anyone help with how i can achieve this (retrieving the edittexts from several fragments i add dynamically)? Thanks.

Here is the Code:

public class BasicDesign extends AppCompatActivity {

TextView ve;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.instance_shpw);

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "Third");
    transaction.addToBackStack(null);
    transaction.commit();

}

public void DispText(View view) {

    //Works only for the first fragment
    //I would want it to work with other fragments when other fragments are added.

    TextView textview = (TextView) findViewById(R.id.DisplayText);
    EditText dt = (EditText) findViewById(R.id.Edt);
    String shw=dt.getText().toString();
    textview.setText(shw);

}

public void functionAdd(View view) {

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "winter");
    transaction.addToBackStack("winter");
    transaction.commit();
}

public static class det extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dynamic_fragment, container, false);

    }
}
}

The dynamic_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:background="#979797">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.8"
    android:padding="20dp"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:id="@+id/Edt" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:padding="10dp"
    android:layout_weight="0.2"
    android:textStyle="bold"
    style="@style/Base.TextAppearance.AppCompat.Large"
    android:id="@+id/adder"
    android:onClick="functionAdd" />

</LinearLayout>

Instance_shpw.xml

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragmentContainer"
    android:orientation="vertical">


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display Text"
        android:id="@+id/button4"
        android:layout_gravity="center_horizontal"
        android:onClick="DispText" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:id="@+id/DisplayText"
        android:layout_gravity="center_horizontal"
        android:padding="16dp"/>


</LinearLayout>


</LinearLayout>

Upvotes: 0

Views: 99

Answers (2)

user3386180
user3386180

Reputation:

You should create event callbacks from the fragment to the parent activity. Have a look at the Android developer guide here.

Roughly like this:

public static class det extends Fragment {

     OnItemChangedListener mListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dynamic_fragment, container, false);
    }

    // Define the interface to be implemented by the activity.
    public interface OnViewChangedListener{
        public void onViewChanged(String value);
    }

    // Instantiate the interface.
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnViewChangedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnViewChangedListener");
        }
    }

    // Use the interface (i.e. call the method in the activity)
    // where you need it. For instance when your editText is changed:
    editText.addTextChangedListener(new TextWatcher() {
       public void afterTextChanged(Editable s) {
       }
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
       }
       public void onTextChanged(CharSequence s, int start, int before, int count) {
           mListener.onViewChanged(s);
       }
   });    
}

and in the parent activity:

public class BasicDesign extends AppCompatActivity 
    implements det.OnViewChangedListener {

    //...

    // Implement the interface in the activity.
    @Override
    public void onViewChanged(String value) {
        // Use the retrieved value, e.g. save this to a local
        // that you can show when you click your button.
    }
}

Upvotes: 1

Annada
Annada

Reputation: 1085

You can retrieve inputs from run time added fragment, by using getting viewid of the particular fragment instance. In the above code, it is getting (EditText) findViewById(R.id.Edt) from first fragment instance only.

public class BasicDesign extends AppCompatActivity {
TextView ve;
private String recentFragmentTag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.instance_shpw);

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "Third");
    transaction.addToBackStack(null);
    transaction.commit();

    recentFragmentTag = "Third";
}

public void DispText(View view) {

    //Works only for the first fragment
    //I would want it to work with other fragments when other fragments are added.

    TextView textview = (TextView) findViewById(R.id.DisplayText);
    //EditText dt = (EditText) findViewById(R.id.Edt);
    Fragment recentFragment = getFragmentManager().findFragmentByTag(recentFragmentTag);
    EditText dt = (EditText)recentFragment.getView().findViewById(R.id.Edt);
    String shw=dt.getText().toString();
    textview.setText(shw);

}

public void functionAdd(View view) {

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "winter");
    transaction.addToBackStack("winter");
    transaction.commit();

    recentFragmentTag = "winter";
}

public static class det extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dynamic_fragment, container, false);

    }
  }
}

Upvotes: 0

Related Questions