0

I cannot get my app to successfully send data from one fragment to another.

MainActivity.java which sets up my fragment tabs

package myPackage;

public class MainActivity extends AppCompatActivity {

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

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_HOME));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_PERSON_INFO));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_3));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_4));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_5));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_6));
        tabLayout.addTab(tabLayout.newTab().setText(Constants.TAB_7));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

PageAdapter.java

package myPackage;

public class PagerAdapter extends FragmentStatePagerAdapter {

    int numberOfTabs;

    public PagerAdapter(FragmentManager fm, int numberOfTabs) {
        super(fm);
        this.numberOfTabs = numberOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                Home home = new Home();
                return home;
            case 1:
                PersonInformation personInformation = new PersonInformation();
                return personInformation;
            case 2:
                Tab3 tab3 = new Tab3();
                return tab3;
            case 3:
                Tab4 tab4 = new Tab4();
                return tab4;
            case 4:
                Tab5 tab5 = new Tab5();
                return tab5;
            case 5:
                Tab6 tab6 = new Tab6();
                return tab6;
            case 6:
                Tab7 tab7 = new Tab7();
                return tab7;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return numberOfTabs;
    }
}

Sending Fragment

package myPackage;

public class Home extends Fragment {

    private View rootView;
    private DatabaseHelper myDBHelper;
    private Cursor personCursor;
    private SimpleCursorAdapter mySimpleCursorAdapter;
    private Activity myActivity;

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

        myDBHelper = new DatabaseHelper(getActivity());

        personCursor = myDBHelper.getPersonCursor();
        String[] fromColumns = {"personID"};
        int[] toViews = {R.id.person_id_textview};
        mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.person_layout, personCursor, fromColumns, toViews, 0);

        ListView myListView = (ListView) rootView.findViewById(R.id.person_row);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor subCursor = (Cursor) mySimpleCursorAdapter.getItem(position);
                String personIDNumber = subCursor.getString(subCursor.getColumnIndex("personID"));

                PersonInformation personInformation = new PersonInformation();
                Bundle myBundle = new Bundle();
                myBundle.putString("personID", personIDNumber);
                personInformation.setArguments(myBundle);
                getFragmentManager().beginTransaction().add(R.id.row_of_information, personInformation).commit();
            }
        });

        myListView.setAdapter(mySimpleCursorAdapter);

        myDBHelper.close();

        return rootView;
    }
}

Receiving Fragment

package myPackage;

public class PersonInformation extends Fragment {

    private View rootView;
    private DatabaseHelper myDBHelper;
    private Cursor informationCursor;
    private SimpleCursorAdapter mySimpleCursorAdapter;

    private TextView personIDDisplay;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        rootView = inflater.inflate(R.layout.person_information, container, false);

        personID = getArguments().getString("personID");

        myDBHelper = new DatabaseHelper(getActivity());

        informationCursor = myDBHelper.getInformationCursor(personID);
        String[] fromColumns = {"firstname", "lastname", "homephone", "homeaddress"};
        int[] toViews = {R.id.firstname_textview, R.id.lastname_textview, R.id.homephone_textview, R.id.homeaddress_textview};
        mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.information_layout, informationCursor, fromColumns, toViews, 0);

        personIDDisplay = (TextView) rootView.findViewById(R.id.person_id_display);
        personIDDisplay.setText("Person ID: " + personID);

        ListView myListView = (ListView) rootView.findViewById(R.id.row_of_information);
        myListView.setAdapter(mySimpleCursorAdapter);

        myDBHelper.close();

        return rootView;
    }
}

person_information.xml Layout

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

    <TextView android:id="@+id/person_id_display" style="@style/personIDDisplay" />

    <include layout="@layout/person_table_header" />

    <ListView
        android:id="@+id/row_of_information"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

I either get

addView(View) not supported in AdapterView

when I use

getFragmentManager().beginTransaction().add(R.id.row_of_information, personInformation).commit();
// notice the R.id.row_of_information

Which is my ListView of the person information, or I get a NullPointerException in my receiving class, on the arguments if I use the ListView parent view ID.

I have tried enclosing the receiving ListView in another layout and sending to that ID instead of the ListView's ID, but that hasn't worked either. All examples of how to send data from one fragment to another seem very similar, so I cannot figure out why mine is not working.

Brian
  • 1,726
  • 2
  • 24
  • 62

1 Answers1

0

Warning: Not the best way but would do for now. Better solution would be to use Interface and use them as callback. See Passing data between two Fragments in a VIewPager (Android) (NullPointerException) and Passing data beetwen fragments in viewpager

Change PersonInformation like this

public class PersonInformation extends Fragment {
    private View rootView;
    private DatabaseHelper myDBHelper;
    private Cursor informationCursor;
    private SimpleCursorAdapter mySimpleCursorAdapter;
    private TextView personIDDisplay;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        rootView = inflater.inflate(R.layout.person_information, container, false);
        return rootView;
    }

    public void init(string personId)
    {
        myDBHelper = new DatabaseHelper(getActivity());
        informationCursor = myDBHelper.getInformationCursor(personId);
        String[] fromColumns = {"firstname", "lastname", "homephone", "homeaddress"};
        int[] toViews = {R.id.firstname_textview, R.id.lastname_textview, R.id.homephone_textview, R.id.homeaddress_textview};
        mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.information_layout, informationCursor, fromColumns, toViews, 0);
        personIDDisplay = (TextView) rootView.findViewById(R.id.person_id_display);
        personIDDisplay.setText("Person ID: " + personId);
        ListView myListView = (ListView) rootView.findViewById(R.id.row_of_information);
        myListView.setAdapter(mySimpleCursorAdapter);
        myDBHelper.close();
    }
}

Replace

PersonInformation personInformation = new PersonInformation();
Bundle myBundle = new Bundle();
myBundle.putString("personID", personIDNumber);
personInformation.setArguments(myBundle);
getFragmentManager().beginTransaction().add(R.id.row_of_information, personInformation).commit();

with

String tag = "android:switcher:" + R.id.pager + ":" + "1";
PersonInformation f = (PersonInformation) getSupportFragmentManager().findFragmentByTag(tag);
f.init(personIDNumber);

in onItemClick of Home fragment

Community
  • 1
  • 1
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57