Fragment 中带有自定义适配器的 ListView

ListView with custom adapter in Fragment

我正在尝试在 fragmnet 中将 ListView 与自定义适配器 (baseAdapter) 结合使用。

当我直接在 MainActivity 中使用此代码时,一切正常,但当我在片段中使用此代码时,它没有崩溃,但它没有显示任何内容,它只是一个空白片段。此外,当我尝试使用简单的 arrayAdapter 在片段中绑定一个 textView 时,它工作正常 所以我认为问题出在我的自定义适配器中。

为什么不显示 ListView?

我的代码: 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>

编辑:

如果我将 OnCreateView 方法替换为 return: return V;我收到 NPE 错误:

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)

因为 List<codeLearnChapter> codeLearnChapterList = getDataForListView(); 这一行 getDataForListView() 不会填充列表数据,所以你必须在构造函数初始化时调用它。

只需将构造函数添加到您的自定义适配器 CodeLearnAdapter

喜欢

List<codeLearnChapter> codeLearnChapterList ;

public CodeLearnAdapter () {

       codeLearnChapterList = getDataForListView();
}

并且在 onCreateView 中你必须 return View V,因为它总是膨胀新创建的视图所以没有任何列表。

@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;
}

从堆栈跟踪中,我怀疑 mImageLoader 可以为 null,因为 activity 没有获取 Context 的引用,所以替换

// 通过你的单例获取ImageLoader class.

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

// 通过你的单例获取ImageLoader class.

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

我看到两个问题:

  • 首先,您的 onCreateView() 方法有问题。您将布局 (R.layout.fragment_home) 膨胀两次。该方法的最后一行应该是 return v; 而不是 return inflater.inflate(R.layout.fragment_home, container, false);
  • 其次,在 class 初始化时调用适配器 List<codeLearnChapter> codeLearnChapterList = getDataForListView(); 中的第一行。通常的做法是在适配器的构造函数中发送数据:

    public CodeLearnAdapter () {
          this.codeLearnChapterList = getDataForListView();
    }