在Fragment中使用自定义适配器的ListView

时间:2015-02-27 19:34:00

标签: android listview android-fragments fragment baseadapter

我试图在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 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)

2 个答案:

答案 0 :(得分:2)

由于此行List<codeLearnChapter> codeLearnChapterList = getDataForListView();上的getDataForListView()不会填充List数据,因此必须在构造函数的初始化时调用它。

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

即可

List<codeLearnChapter> codeLearnChapterList ;

public CodeLearnAdapter () {

       codeLearnChapterList = getDataForListView();
}

onCreateView中,您必须返回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,因为活动没有得到Context的引用所以替换

//通过单件类获取ImageLoader。

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

//通过单件类获取ImageLoader。

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

答案 1 :(得分:1)

我看到两个问题:

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

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