ListView with custom adapter in Fragment

11,100

Solution 1

Because List<codeLearnChapter> codeLearnChapterList = getDataForListView(); on this line getDataForListView() will not going to fill List data, you have to call this on initialize of constructor.

Just add constructor to your custom adapter CodeLearnAdapter

Like

List<codeLearnChapter> codeLearnChapterList ;

public CodeLearnAdapter () {

       codeLearnChapterList = getDataForListView();
}

And in onCreateView you have to return View V, as It always inflated newly created view so there is no any List with it.

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

    View V = inflater.inflate(R.layout.fragment_home, container, false);

    CodeLearnAdapter chapterListAdapter = new CodeLearnAdapter();
    ListView codeLearnLessons = (ListView) V.findViewById(R.id.listView);
    codeLearnLessons.setAdapter(chapterListAdapter);

    return V;
}

From the stacktrace, I have doubt of mImageLoader can be null, as activity doesn't get reference of Context so Replace

// Get the ImageLoader through your singleton class.

mImageLoader = VolleySingleton.getInstance(activity).getImageLoader();

with

// Get the ImageLoader through your singleton class.

mImageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();

Solution 2

I see two problems:

  • First, you have a problem on your onCreateView() method. You inflate your layout (R.layout.fragment_home) twice. The last line in the method should be return v; instead of return inflater.inflate(R.layout.fragment_home, container, false);
  • Second, the first line in your adapter List<codeLearnChapter> codeLearnChapterList = getDataForListView(); is called upon class initialization. It is common practice to send the data in the constructor of your Adapter:

    public CodeLearnAdapter () {
          this.codeLearnChapterList = getDataForListView();
    }
    
Share:
11,100
Alex Mensak
Author by

Alex Mensak

SMILEUPPS-5A85E14BA4

Updated on June 04, 2022

Comments

  • Alex Mensak
    Alex Mensak almost 2 years

    Im trying to use ListView with custom adapter (baseAdapter) in fragmnet.

    When I us this code directly inside the MainActivity everything works fine, but when I use this in fragment it did't crash but it din't show anything, it is just a blank fragment. Also when I've tried to use simple arrayAdapter to bind one textView in fragment it worked fine so the problem will be inside my custom adapter I think.

    Why is it not displaying the ListView?

    My code: HomeFragment.java

    package com.example.alex.viewlist;
    
    import android.app.Fragment;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import com.android.volley.toolbox.ImageLoader;
    import com.android.volley.toolbox.NetworkImageView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class HomeFragment extends Fragment {
    
        ImageLoader mImageLoader;
        NetworkImageView mNetworkImageView;
    
        public  MainActivity activity = (MainActivity) getActivity();
    
        public HomeFragment() {
            // Required empty public constructor
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View V = inflater.inflate(R.layout.fragment_home, container, false);
    
            CodeLearnAdapter chapterListAdapter = new CodeLearnAdapter();
            ListView codeLearnLessons = (ListView) V.findViewById(R.id.listView);
            codeLearnLessons.setAdapter(chapterListAdapter);
    
            return inflater.inflate(R.layout.fragment_home, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
        }
    
        public class codeLearnChapter {
            String mURL;
            String mName;
        }
    
        String[] ListURL = new String[] {"http://meteo.profi-net.sk/webcams/BIELAPUT-l.jpg",
                "http://meteo.profi-net.sk/webcams/Priehyba-l.jpg",
                "http://meteo.profi-net.sk/webcams/JASNA_CHOPOK-l.jpg",
                "http://meteo.profi-net.sk/webcams/LM_JASNA_LUKOVA-l.jpg",
                "http://meteo.profi-net.sk/webcams/brhliska-l.jpg"};
    
        String[] ListName = new String[] {"Biela púť",
                "Priehyba",
                "Chopok",
                "Luková",
                "Grand Jet"};
    
        public List<codeLearnChapter> getDataForListView()
        {
            List<codeLearnChapter> codeLearnChaptersList = new ArrayList<codeLearnChapter>();
    
            for(int i=0;i<ListURL.length;i++)
            {
    
                codeLearnChapter chapter = new codeLearnChapter();
                chapter.mURL = ListURL[i];
                chapter.mName = ListName[i];
                codeLearnChaptersList.add(chapter);
            }
    
            return codeLearnChaptersList;
    
        }
    
        public class CodeLearnAdapter extends BaseAdapter {
    
            List<codeLearnChapter> codeLearnChapterList = getDataForListView();
            @Override
            public int getCount() {
                return codeLearnChapterList.size();
            }
    
            @Override
            public codeLearnChapter getItem(int arg0) {
                return codeLearnChapterList.get(arg0);
            }
    
            @Override
            public long getItemId(int arg0) {
                return arg0;
            }
    
            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {
    
                if(arg1==null)
                {
                    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    arg1 = inflater.inflate(R.layout.view_list, arg2,false);
                }
    
                TextView chapterName = (TextView)arg1.findViewById(R.id.textView1);
    
                codeLearnChapter chapter = codeLearnChapterList.get(arg0);
    
                String url = chapter.mURL;
                chapterName.setText(chapter.mName);
    
                mNetworkImageView = (NetworkImageView) arg1.findViewById(R.id.NetView);
    
                // Get the ImageLoader through your singleton class.
                mImageLoader = VolleySingleton.getInstance(activity).getImageLoader();
    
                // Set the URL of the image that should be loaded into this view, and
                // specify the ImageLoader that will be used to make the request.
                mNetworkImageView.setImageUrl(url, mImageLoader);
                return arg1;
            }
    
        }
    }
    

    fragment_home.xml

    <FrameLayout 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" tools:context="com.example.alex.viewlist.HomeFragment">
    
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    

    viewList.xml

    <view
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    class="android.support.v7.widget.CardView"
    android:id="@+id/view"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="4dp"
    android:paddingTop="4dp"
    android:paddingRight="4dp"
    android:paddingBottom="4dp">
    <RelativeLayout
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical">
    
        <view
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            class="com.android.volley.toolbox.NetworkImageView"
            android:id="@+id/NetView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView1"
            android:layout_gravity="left|bottom"
            android:textSize="22dp"
            android:layout_below="@+id/NetView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffd4d4d4"
            android:textColor="#ff272727"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"/>
    </RelativeLayout>
    

    EDIT:

    if I replece OnCreateView method to return: return V; i get NPE error:

    02-27 20:52:15.955  28063-28063/com.example.alex.viewlist E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.alex.viewlist, PID: 28063
    java.lang.NullPointerException
            at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43)
            at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
            at com.example.alex.viewlist.VolleySingleton.<init>(VolleySingleton.java:18)
            at com.example.alex.viewlist.VolleySingleton.getInstance(VolleySingleton.java:39)
            at com.example.alex.viewlist.HomeFragment$CodeLearnAdapter.getView(HomeFragment.java:133)
            at android.widget.AbsListView.obtainView(AbsListView.java:2263)
            at android.widget.ListView.makeAndAddView(ListView.java:1790)
            at android.widget.ListView.fillDown(ListView.java:691)
            at android.widget.ListView.fillFromTop(ListView.java:752)
            at android.widget.ListView.layoutChildren(ListView.java:1630)
            at android.widget.AbsListView.onLayout(AbsListView.java:2091)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2066)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1823)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1079)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5784)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:774)
            at android.view.Choreographer.doCallbacks(Choreographer.java:587)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:760)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
    
  • Alex Mensak
    Alex Mensak about 9 years
    Thx for help, but it still not showing my listView. Also, it was working without it before when the same code was used in Activity not Fragment.
  • Alex Mensak
    Alex Mensak about 9 years
    when i use return V; app gets NullPointerException. And that constructor doesn't solved my problem. Whats wrong?
  • Alex Mensak
    Alex Mensak about 9 years
    WoW after replacing that mImageLoader ist working fine. THX, but can you explain me why it wasnt woring, what was the problem?
  • user370305
    user370305 about 9 years
    @AlexMensak - Cool.. Happy coding. Just be sure at the class level you can't execute methods excepts they are static. So in your case public MainActivity activity = (MainActivity) getActivity(); its not working and also there is no need of Fragment constructor :)