在Android的新Fragment标准中加载listView而不是Activity

时间:2014-05-10 19:00:07

标签: android android-fragments android-listview

今天,当我看到构建活动的正常方式发生了变化时,我正在制作一个新的应用程序。我试图用我的数据库中的数据填充列表视图,但我无法让它工作。 rootview是我猜的问题。

以下代码是一个旧的活动示例,它可以正常工作(我测试过它):

public class TrackNewActivity extends Activity {

    List<TrackSave> tracks = new ArrayList<TrackSave>();

    private DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_track_save);

        db = new DatabaseHelper(this);
        tracks = db.getAllSavedTracks();
        fillTracks();
    }

    private void fillTracks() {
        TrackAdapter trackAdapter = new TrackAdapter(getApplicationContext(),
                tracks);
        final ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(trackAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parentView, View childView,
                    int position, long id) {
            }
        });
    }
}

请注意,setContentView设置为我实际想要使用的片段视图。这个 fragment_track_save.java 只包含一个带有listview的Linearlayout,用于显示项目。 不幸的是,下面的代码在我试图获得与旧活动示例相同的地方不起作用:

public class TrackSaveActivity extends ActionBarActivity {

    List<TrackSave> tracks = new ArrayList<TrackSave>();

    static View rootView;
    private DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_track);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        db = new DatabaseHelper(this);
        tracks = db.getAllSavedTracks();
        fillTracks();
    }

    private void fillTracks() {
        TrackAdapter trackAdapter = new TrackAdapter(getApplicationContext(),
                tracks);
        final ListView listView = (ListView) rootView
                .findViewById(R.id.listView);
        listView.setAdapter(trackAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parentView, View childView,
                    int position, long id) {
            }
        });
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.fragment_track_save,
                    container, false);

            return rootView;
        }
    }
}

当我尝试查找列表视图时,会出现此问题。它总是引用一个nullpointer异常,因为它无法找到listview,这是我的猜测。

activity_track.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="*.TrackActivity"
    tools:ignore="MergeRootFrame" />

希望我说清楚。

1 个答案:

答案 0 :(得分:1)

如果你想使用一个片段,你应该有MainActivity这样的东西:

    public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (savedInstanceState == null) {
                getFragmentManager().beginTransaction()
                        .add(R.id.container, new MyFragment())
                        .commit();
            }
        }
    }

然后,Fragment的一个类,在本例中为ListFragment

    public class MyFragment extends ListFragment {

        public MyFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            ArrayList<String> la = new ArrayList<String>();
            la.add("Item1");
            la.add("Item2");

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, la);

            setListAdapter(adapter);
            return rootView;
        }

fragment_main应该是这样的:

    <LinearLayout 
        // ....>

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>