Search code examples
androidandroid-fragmentsandroid-adapterview

Why is my Android AdapterView not working after moving it to a fragment?


I am working on an app that reads data from a database, and displays it in a ListView. This was working well until I moved my code from the MainActivity.java to a fragment. Now, not so much. I don't get any errors, but the list does not populate like when the code was in the main activity. This is what I had before fragments:

data_view.xml

<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/top_linear_layout">

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

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

    </LinearLayout>

</RelativeLayout>

from MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        myDBHelper = new DatabaseHelper(this);

        dataCursor = myDBHelper.getDataCursor();
        String[] fromColumns = {"time", "col1", "col2", "col3", "col4", "col5"};
        int[] toViews = {R.id.time_textview, R.id.col1_textview, R.id.col2_textview, R.id.col3_textview, R.id.col4_textview, R.id.col5_textview};
        mySimpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.my_data_row_layout, dataCursor, fromColumns, toViews, 0);

        ListView myListView = (ListView) findViewById(R.id.row_of_stats);
        myListView.setAdapter(mySimpleCursorAdapter);
}

Now, this is what my new fragment looks like, that does not work:

package mypackage.main;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import mypackage.R;

public class MyFragment extends Fragment {

    private View rootView;
    private DatabaseHelper myDBHelper;
    private Cursor dataCursor;

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

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

        myDBHelper = new DatabaseHelper(getActivity());

        dataCursor = myDBHelper.getDataCursor();
        String[] fromColumns = {"time", "col1", "col2", "col3", "col4", "col5"};
        int[] toViews = {R.id.time_textview, R.id.col1_textview, R.id.col2_textview, R.id.col3_textview, R.id.col4_textview, R.id.col5_textview};
        mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.my_data_row_layout, dataCursor, fromColumns, toViews, 0);

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

        return inflater.inflate(R.layout.data_view, container, false);
    }
}

Like I said, the AdapterView worked perfectly, and showed the list of data, but now it is just empty with no errors.


Solution

  • You are infalting your layout twice. Change :

    return inflater.inflate(R.layout.data_view, container, false);
    

    to

    return rootView;