【问题标题】:How do ListFragments work with Loader and Custom Cursor Adapter?ListFragments 如何与加载器和自定义光标适配器一起使用?
【发布时间】:2015-05-22 23:21:35
【问题描述】:

我有一个 ListFragment,它通过加载器加载其数据。 到目前为止,我使用了带有 SimpleCursorAdapter 的内置 R.layout.list_item。 但是现在,我必须对每个列表项进行一些自定义格式设置,并且需要使用自定义列表项布局,所以我必须转换我的 ListFragment 以使用我的自定义光标适配器。 我不明白的是,如何绑定异步加载到我的列表视图的数据。到目前为止,我拥有的是我的 ListFragment 和 GewichtListViewAdapter。

这是我的列表片段:

public class GewichtListViewAdapter extends CursorAdapter {

public GewichtListViewAdapter(Context context, Cursor c, boolean autoRequery) {
    super(context, c, autoRequery);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.listview_item_gewicht, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    // Find fields to populate in inflated template
    TextView gewichtTextView = (TextView)view.findViewById(R.id.gewichtTextView);
    TextView gemessenAmTextView = (TextView)view.findViewById(R.id.gemessenAmTextView);

    // Extract properties from cursor
    String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
    int priority = cursor.getInt(cursor.getColumnIndexOrThrow("priority"));

    // Populate fields with extracted properties
    long geburtsdatumLong = cursor.getLong(cursor.getColumnIndex(GewichtContract.Columns.GEMESSEN_AM));
    LocalDate geburtsDatumLocalDate = new LocalDate(geburtsdatumLong);
    DateTimeFormatter fmt = DateTimeFormat.mediumDate();
    String str = fmt.print(geburtsDatumLocalDate);
    gemessenAmTextView.setText(str);

    double gewichtDouble = cursor.getDouble(cursor.getColumnIndex(GewichtContract.Columns.GEWICHT));
    BigDecimal gewichtBigDecimal = new BigDecimal(gewichtDouble, MathContext.DECIMAL32);
    gewichtTextView.setText(String.format("%.1f", gewichtBigDecimal));

    }
}

这是我的 ListFragment 与 Loader 的摘录:

public class GewichtListFragment extends ListFragment implements
    LoaderManager.LoaderCallbacks<Cursor>{



@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        String[] projection = { GewichtContract.Columns._ID, GewichtContract.Columns.GEMESSEN_AM, GewichtContract.Columns.GEWICHT };

        CursorLoader cursorLoader = new CursorLoader(getActivity(),
        GewichtContract.CONTENT_URI, projection, null, null, null);
        return cursorLoader;
        }

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.swapCursor(cursor);
        }

@Override
public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
        }




@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
        }

        // TODO: Change Adapter to display your content
        //setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
        //        android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS));

        String[] uiBindFrom = { GewichtContract.Columns.GEWICHT, GewichtContract.Columns.GEMESSEN_AM };
        int[] uiBindTo = { R.id.gewichtTextView, R.id.gemessenAmTextView };

        getLoaderManager().initLoader(GEWICHT_LIST_LOADER, null, this);

        adapter = new SimpleCursorAdapter(
            getActivity(), R.layout.listview_item_gewicht, null, uiBindFrom, uiBindTo,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        setListAdapter(adapter);
        }


}

如果使用简单的光标适配器,它会附加在 onCreate() 中。但是,我需要对光标的引用,而我在 onCreate() 中没有。此外,此时日期(据我所知)尚未加载,因此我看不到如何在此处绑定/附加。 谁能告诉我 ListFragments、自定义光标适配器和加载器如何协同工作?

【问题讨论】:

    标签: android android-listview android-cursoradapter android-cursor android-cursorloader


    【解决方案1】:

    我的解决方案:

    创建一个空的 MatrixCursor 并在 onCreate() 中用它构造我的 GewichtListViewAdapter:

            String[] columnNames = {GewichtContract.Columns._ID,
                    GewichtContract.Columns.GEMESSEN_AM,
                    GewichtContract.Columns.GEWICHT,
                    GewichtContract.Columns.HUND_ID};
            MatrixCursor matrixCursor = new MatrixCursor(columnNames);
            gewichtListViewAdapter = new GewichtListViewAdapter(getActivity(), matrixCursor, true);
            setListAdapter(gewichtListViewAdapter);
    

    在 onCreateLoader() 中:

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    
            String[] projection = { GewichtContract.Columns._ID,
                    GewichtContract.Columns.GEMESSEN_AM,
                    GewichtContract.Columns.GEWICHT,
                    GewichtContract.Columns.HUND_ID
                    };
    
            CursorLoader cursorLoader = new CursorLoader(getActivity(),
            GewichtContract.CONTENT_URI, projection, null, null, GewichtContract.Columns.GEMESSEN_AM);
            return cursorLoader;
            }
    

    【讨论】:

    • 有趣...你的意思是 GewichtListFragment 而不是 GewichtListViewAdapter 在 onCreate() 中?
    • @TheOriginalAndroid 我不相信。我直接从我的项目中复制粘贴了代码,它按原样工作......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多