In the Fragment1 I send a name and a phone to the Fragment2 I use the following example to try to fill the Recycler of the Second Fragment the communication works because I tried it adapting it to my needs, what I want to do now is to fill a Recycler sending said information ..
Send data from a Fragment to Fragment
I do not know if I misunderstood any method to send information from one fragment to another or I was wrong about something
I made changes as needed and tried but I get the following error I made modifications try with several forms and it does not come out ..
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
It should be noted that I already have a Recycler Adapter
Fragment1
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="@string/title_fragmen_dos"
android:textAlignment="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ingrese_nombre"/>
<EditText
android:id="@+id/editTex1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Escribe"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ingrese telefono"/>
<EditText
android:id="@+id/editTex2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Escribe"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<Button
android:id="@+id/btnGuardar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aceptar"/>
</LinearLayout>
</FrameLayout>
Fragment2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tercerFragment"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TercerBlankFragment"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<include
layout="@layout/recycler_contactos"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity
@Override
public void llenadoArray(ArrayList<InfoContactos> arra) {
arrayContact = arra;
viewPager.setCurrentItem(1,true);
}
@Override
public ArrayList<InfoContactos> getArrayContact() {
return arrayContact;
}
In the fragment1
final InfoContactos contacto = new InfoContactos(editText1.getText().toString(),editText2.getText().toString(),"btnTemp");
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addContact(contacto);
listener.llenadoArray(arrayContatos);
//listener.onFechaActualIngresada(editText1.getText().toString());
}
});
private void addContact(InfoContactos contacto){
arrayContatos.add(contacto);
}
public interface FragmentUnoListener{
void llenadoArray(ArrayList<InfoContactos> arra);
}
Fragment2
recyclerContact = view.findViewById(R.id.recicler_contactos);
recyclerContact.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
AdapterRecyclerFragment adapter = new AdapterRecyclerFragment(listener.getArrayContact());
recyclerContact.setAdapter(adapter);
}
}
public interface FragmentDosListener{
ArrayList<InfoContactos> getArrayContact();
}