在Fragment中设置ListView的适配器

4

我是Android的新手,试图将先前在我的主Activity中控制的listview转移到一个fragment中。

我的MainActivity扩展了FragmentActivity。我得到了一个空指针,用于设置适配器和包含populateList()方法的行。

不确定解决方案是否与getActivity()和/或我一直在阅读的setListAdapter()选项有关。我知道它正在从保存中找到项目,但我不确定如何解决这个问题。任何见解都将不胜感激。

附加信息: 我正在使用android.support.v4.app.FragmentActivityFragmentS位于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>

  1. 为什么你要在 onCreateView()populateList() 中实例化你的适配器两次?
  2. onCreateView() 方法之前放置 List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
- Rami
很不幸,我确实有。java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' - Austin
请确保您有一个ID为saveListViewListView对象,位于listview_s.xml中。 - Rami
存在,并且ID匹配。FragmentS包含在另一个名为public class TabbedActivity extends Fragment {的类中。不确定是否有影响。对我的格式不熟悉,抱歉。 - Austin
你能发一下你的 listview_s.xml 文件吗? - Rami
显示剩余2条评论
1个回答

6
我认为问题在于你在Activity中搜索ListView,而不是在onCreateView()方法中检索到的rootView。
也许尝试这样做:
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);
            }

...


好的想法。saveListView = rootView.findViewById(R.id.saveListView);android.view.View 传递了进去,但是需要的是 android.widget.ListView。我将其转化类型后,它就起作用了,列表视图得到了填充。非常感谢这个帮助。然而,列表中的条目被截断了一半,这是我从未见过的。 - Austin
太好了!也许可以开一个新的问题,针对这个具体的问题,并确保添加屏幕截图以显示条目被如何裁剪。 - rubengees
我在上面发布XML后一定进行了更改。哈哈。现在已经修复好了,一切都正常工作。感谢@Rami,特别感谢@RubyDerBoss! - Austin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接