为什么我的Android AdapterView在将其移动到片段后无法正常工作?

时间:2016-01-05 16:18:36

标签: android android-fragments android-adapterview

我正在开发一个从数据库中读取数据的应用程序,并将其显示在ListView中。这很好用,直到我将代码从MainActivity.java移动到片段。现在,不是那么多。我没有收到任何错误,但列表不会像代码在主要活动中那样填充。这就是我之前片段:

data_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/top_linear_layout">

        <include layout="@layout/table_header" />

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

    </LinearLayout>

</RelativeLayout>
来自MainActivity.java的

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        myDBHelper = new DatabaseHelper(this);

        dataCursor = myDBHelper.getDataCursor();
        String[] fromColumns = {"time", "col1", "col2", "col3", "col4", "col5"};
        int[] toViews = {R.id.time_textview, R.id.col1_textview, R.id.col2_textview, R.id.col3_textview, R.id.col4_textview, R.id.col5_textview};
        mySimpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.my_data_row_layout, dataCursor, fromColumns, toViews, 0);

        ListView myListView = (ListView) findViewById(R.id.row_of_stats);
        myListView.setAdapter(mySimpleCursorAdapter);
}

现在,这就是我的新片段的样子,但不起作用:

package mypackage.main;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import mypackage.R;

public class MyFragment extends Fragment {

    private View rootView;
    private DatabaseHelper myDBHelper;
    private Cursor dataCursor;

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

        rootView = inflater.inflate(R.layout.data_view, container, false);

        myDBHelper = new DatabaseHelper(getActivity());

        dataCursor = myDBHelper.getDataCursor();
        String[] fromColumns = {"time", "col1", "col2", "col3", "col4", "col5"};
        int[] toViews = {R.id.time_textview, R.id.col1_textview, R.id.col2_textview, R.id.col3_textview, R.id.col4_textview, R.id.col5_textview};
        mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.my_data_row_layout, dataCursor, fromColumns, toViews, 0);

        ListView myListView = (ListView) rootView.findViewById(R.id.row_of_stats);
        myListView.setAdapter(mySimpleCursorAdapter);

        return inflater.inflate(R.layout.data_view, container, false);
    }
}

就像我说的那样,AdapterView工作得很好,并显示了数据列表,但现在它只是空的,没有错误。

1 个答案:

答案 0 :(得分:4)

你正在两次渗透你的布局。 变化:

return inflater.inflate(R.layout.data_view, container, false);

return rootView;