Set Adapter In Fragment for ListView

42,364

I think the Problem is, that you search your Activity for the ListView but not the rootView you retrieved in onCreateView()

Maybe try this:

public static class FragmentS extends Fragment {

            private ListView saveListView;

            private List<LiftSave> LiftSaves = new ArrayList<LiftSave>();

            public FragmentS() {

            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.listview_s,
                        container, false);
                saveListView = (ListView) rootView.findViewById(R.id.saveListView);

                DatabaseHandler dbHandler;
                dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
                if (dbHandler.getLiftSavesCount() != 0)
                    LiftSaves.addAll(dbHandler.getAllLiftSaves());

                populateList();
                return rootView;
            }

            private void populateList() {
                ArrayAdapter<LiftSave> saveAdapter = new SaveListAdapter();

                saveListView.setAdapter(saveAdapter);
            }

...

Share:
42,364
Austin
Author by

Austin

Updated on April 06, 2020

Comments

  • Austin
    Austin about 4 years

    I'm new to Android. Trying to get a listview previously controlled in my Main Activity to a fragment.

    My MainActivity extends FragmentActivity. I get a null pointer for the set adapter, and for the line containing the populateList() method.

    Not sure if the solution is with getActivity() and/or the setListAdapter() option I've been reading about. I know that it is finding the items from the saves, but I'm not sure how to correct this issue. Any insight would be greatly appreciated.

    Additional Info: I am using android.support.v4.app.FragmentActivity. FragmentS is within public class TabbedActivity extends Fragment {

    public static class FragmentS extends Fragment {
    
    
    
            public FragmentS() {
    
            }
            List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.listview_s,
                        container, false);
    
                DatabaseHandler dbHandler;
                dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
                if (dbHandler.getLiftSavesCount() != 0)
                    LiftSaves.addAll(dbHandler.getAllLiftSaves());
    
                populateList();
                return rootView;
            }
            private void populateList() {
                ListView saveListView = (ListView)getActivity().findViewById(R.id.saveListView);
                ArrayAdapter<LiftSave> saveAdapter;
                saveAdapter = new SaveListAdapter();
                saveListView.setAdapter(saveAdapter);
            }
    
            public class SaveListAdapter extends ArrayAdapter<LiftSave> {
                public SaveListAdapter() {
                    super(getActivity(), R.layout.listview_item, LiftSaves);
                }
    
                @Override
                public View getView(int position, View view, ViewGroup parent) {
                    if (view == null)
                        view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false);
    
                    LiftSave currentLiftSave = LiftSaves.get(position);
    
                    TextView liftName = (TextView) view.findViewById(R.id.liftName);
                    liftName.setText(currentLiftSave.getLiftName());
                    TextView maxValue = (TextView) view.findViewById(R.id.maxValue);
                    maxValue.setText(currentLiftSave.getMaxValue());
                    TextView weightAndReps = (TextView) view.findViewById(R.id.weightAndReps);
                    weightAndReps.setText(currentLiftSave.getRepsAndWeight());
                    TextView liftNotes = (TextView) view.findViewById(R.id.liftNotes);
                    liftNotes.setText(currentLiftSave.getLiftNotes());
                    TextView date = (TextView) view.findViewById(R.id.todayDate);
                    date.setText(currentLiftSave.getTodayDate());
    
                    return view;
                }
    
        }}
    

    XML:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".TabbedActivity$FragmentS"
        android:background="@android:color/holo_blue_dark">
    
    <LinearLayout
    android:id="@+id/tabSaveList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Saved Maxes"
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textColor="#fffaf4a1"
        android:textStyle="bold" />
    
    <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/saveListView" />
    
    
    </LinearLayout>
    </RelativeLayout>
    
    • Rami
      Rami about 9 years
      1) Why you instanciate your adapter twice (in onCreateView()and then in populateList() ) ? ---- 2) Put List<LiftSave> LiftSaves = new ArrayList<LiftSave>(); before onCreateView() method
    • Austin
      Austin about 9 years
      Haha, mostly out of confusion. I moved List<LiftSave> as you mentioned before onCreateView() . Thanks for your reply.
    • Austin
      Austin about 9 years
      Unfortunately, I do. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapte‌​r)' on a null object reference
    • Rami
      Rami about 9 years
      Make sure you have a ListView object with saveListView as id, in listview_s.xml
    • Austin
      Austin about 9 years
      There is, and the Id matches. FragmentS is contained in another class called public class TabbedActivity extends Fragment {. Not sure If that matters. Sorry for my formatting, not used to posting.
    • Rami
      Rami about 9 years
      Can you post your listview_s.xml?
    • Austin
      Austin about 9 years
      XML is up. Also mentioned that I'm using android.support.v4.app.FragmentActivity
  • Austin
    Austin about 9 years
    Good thought. saveListView = rootView.findViewById(R.id.saveListView); is passing android.view.View, where android.widget.ListView is required. I cast it, and It worked. The ListView did populate. Many thanks for that. However, the entries are cut in half in the list like I've never seen.
  • rubengees
    rubengees about 9 years
    Great to hear! Maybe open another question with that specific problem and be sure to add a screenshot to show how exactly the entries are being cut.
  • Austin
    Austin about 9 years
    I must have changed my XML after I posted it above. Haha. Its fixed and everything is working. Thanks @Rami and especially @RubyDerBoss!