简体   繁体   中英

Cannot get Android tabs with swipable view to correctly pass information from one tab to another and display it correctly

EDIT: Thanks to @ankit aggarwal, I got one part working, which is TAB 1 not switching to TAB 2. Still need assistance on the view not refreshing in TAB 2.


I am working on an app, in which I am using tabs. I based my app on an example I found on the internet here .

What I have is a ListView in TAB 1. What I want to happen is when I click on an item in the list, it will send an ID to TAB 2, where I want to populate another ListView based on that passed data. I am having issues getting this to work properly.

Currently, I have it working so that if I click an item in the list on TAB 1, then TAB 2 does get the data and populates the new list, however it adds to the view on TAB 2 instead of refreshing/overwriting/etc., and the app does not automatically switch to the new tab. This is what I have for the layout in TAB 2 (ie, the receiving tab):

person.xml

<LinearLayout android:id="@+id/person_information_layout">
    <TextView android:id="@+id/header_row" />
    <LinearLayout>
        <TextView android:id="@+id/column1_row_header" />
        <TextView android:id="@+id/column2_row_header" />
        <TextView android:id="@+id/column3_row_header" />
    </LinearLayout>
    <ListView android:id="@+row_of_data"></ListView>
</LinearLayout>

I am populating this list in the receiving tab (TAB 2) with an AdapterView and a database Cursor.

PersonInformation.java

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, 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.person_information, informationCursor, fromColumns, toViews, 0);

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

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

        myDBHelper.close();

        return rootView;
    }
}

This is the person_information layout that gets populated:

person_information.xml

<LinearLayout>
        <TextView android:id="@+id/column1_data" />
        <TextView android:id="@+id/column2_data" />
        <TextView android:id="@+id/column3_data" />
</LinearLayout>

And here is my sending tab (TAB 1)

Home.java

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().replace(R.id.person_information_layout, personInformation).commit();
            }
        });

        myListView.setAdapter(mySimpleCursorAdapter);

        myDBHelper.close();

        return rootView;
    }
}

As you can see, I have a layout with a top information row, then a row of column headers, then the rows of data. What I want is this:

Top Information Row
Column Headers
data row 1
data row 2
...
data row n

Currently, when I click the person in TAB 1, TAB 2 does get the data from TAB 1 and populates a new list correctly, however it draws it under the already existing top information row and column headers, so I have layers:

Top Information Row
Column Headers
Top Information Row
Column Headers
data row 1
data row 2
...
data row n

Also, the tab does not automatically switch from TAB 1 to TAB 2 when I click a row in TAB 1.

Instead of this line in the sending tab (TAB 1)

getFragmentManager().beginTransaction().add(R.id.person_information_layout, personInformation).commit();

I changed it to this line:

getFragmentManager().beginTransaction().add(R.id.row_of_data, personInformation).commit();

But I got the error:

"addView(View) not supported in AdapterView"

From what I understand, I cannot addView to a ListView, so I changed it back to the parent of the ListView, but then I get the duplicate view.

I keep coming back to wanting to change the line in my receiving tab (TAB 2) from

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

to this

LinearLayout myLinearLayout = (LinearLayout) rootView.findViewById(R.id.person_information_layout);

But the cursor adapter will not work with that.

What do I need to do to get this working?

(1)

Currently, when I click the person in TAB 1, TAB 2 does get the data from TAB 1 and populates a new list correctly, however it draws it under the already existing top information row and column headers, so I have layers:

It is happening because this statement instead of switching to tab2, it is adding a new fragment on top of your activity's frame.

getFragmentManager().beginTransaction().add(R.id.person_information_layout, personInformation).commit();

(2)

Also, the tab does not automatically switch from TAB 1 to TAB 2 when I click a row in TAB 1.

You are using a viewpager in this tabs implementation. So to change to tab2 you can do

viewPager.setCurrentItem(2);

NOTE: What you are missing here is a view pager automatically loads a page previous and the next one to a particular page. So when you are in tab1, tab2 would have already been loaded. So your tab2 won't refresh when you click on list item because it would have already loaded.

The way I solved a similar problem is by creating a callback method from the activity to the fragments and a callback method from the fragments to the activity. When I click on a row in the list of fragment1, I save the information in the arguments of that fragment, and call the callback function of the activity. In that function, I retrieve the information passed and load it to the arguments of fragment2 and call the callback function of fragment2. There I retrieve the information passed and update the list of fragment2. I then call tabLayout.getTabAt(newPosition), in the activity, to redisplay the page of fragment2. You also have to make sure that since the views of the fragments are destroyed as one move between pages, to check in the fragment that the list is still populated and if not to restore the items of the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM