Fragment中的CursorLoader返回不兼容

时间:2018-05-11 19:03:19

标签: java android xml

在返回光标加载器的内容时​​,我遇到了一个问题,它显示返回是无能的。我创建了4个片段,其中每个片段都会在activity_main xml中显示内容。以下是我的InventoryFragment和activity_main

的代码
    public class InventoryFragment extends Fragment implements LoaderManager.LoaderCallbacks {

    private static final String LOG_TAG = InventoryFragment.class.getName();
    //create an integer for the book loader
    private static final int STOCK_LOADER = 0;
    PlatformCursorAdapter mCursorAdapter;

    public InventoryFragment(){}

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        getActivity().getSupportLoaderManager().initLoader(STOCK_LOADER, null, this);
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
        View rootView = inflater.inflate(R.layout.activity_main, container, false);
        ListView listView = rootView.findViewById(R.id.list_view);
        listView.setAdapter(mCursorAdapter);
        return rootView;
    }
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        //define the projections that will be used
        String[] projections = {
                StoreEntry._ID,
                StoreEntry.COLUMN_PRODUCT_NAME,
                StoreEntry.COLUMN_PRODUCT_PRICE,
                StoreEntry.COLUMN_PRODUCT_QUANTITY,
                StoreEntry.COLUMN_PRODUCT_WEIGHT,
                StoreEntry.COLUMN_SUPPLIER_NAME,
                StoreEntry.COLUMN_SUPPLIER_NUMBER,
        };
        return new CursorLoader(getActivity(), StoreEntry.CONTENT_URI, projections, null, null, null);
    }

    @Override
    public void onLoadFinished(@NonNull Loader loader, Object data) {

    }

    @Override
    public void onLoaderReset(@NonNull Loader loader) {

    }
}

这是我的activity_main代码,其中将输入数据列表

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.businessplatform.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/display_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RelativeLayout
            android:id="@+id/empty_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <TextView
                android:id="@+id/empty_shelter_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:fontFamily="sans-serif-medium"
                android:paddingTop="16dp"
                android:text="@string/empty_view_title_text"
                android:textAppearance="?android:textAppearanceMedium" />

            <TextView
                android:id="@+id/empty_subtitle_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/empty_shelter_title"
                android:layout_centerHorizontal="true"
                android:fontFamily="sans-serif"
                android:paddingTop="8dp"
                android:text="@string/empty_view_sub_text"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="#A2AAB0" />

        </RelativeLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_margin="16dp"
            android:src="@drawable/ic_action_name" />

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/menu"
        app:headerLayout="@layout/nav_header"
        />

</android.support.v4.widget.DrawerLayout>

我在OnCreateLoader上返回的消息是以下&#34; CursorLoader无法转换为Loader

1 个答案:

答案 0 :(得分:0)

我要感谢Mike支持调整import语句

来自:import android.content.CursorLoader; 致:import android.support.v4.content.CursorLoader;

解决了问题