我有一个ListFragment和一个按钮的布局。当我单击按钮时,将创建一个新的Buchungstag对象并将其存储在数据库中。现在,我假设LoaderManager会注意到这一变化并创建一个新的光标。然后ListFragment应该显示新对象,但是什么也没有发生。有人可以告诉我我错了吗?

这是我的活动的onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.zeiterfassung);
    buchungstagDAO = new BuchungstagDAO(getApplicationContext());

    Button kommen = (Button) findViewById(R.id.zeit_bntKommen);
    kommen.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            LogWL.e(TAG, "button clicked");
            TBuchungstag tag = new TBuchungstag();
            tag.setDatum(new Date());
            tag.setFrueh(true);
            tag.setMittag(false);
            tag.setAbend(true);
            tag.setNebenkosten(0.37d);
            tag.setParkgebuehren(4.50d);
            tag.setNebenkostenKommentar("billig");
            tag.setParkgebuehrenKommentar("parkhaus");
            tag.setAnwender(((WLAuToApplication) getApplication()).anwender);
            tag.setReiseweg("Bochum - Muenster - Bochum");
            tag.setAendDatum(new Date());
            tag = buchungstagDAO.saveOrUpdate(tag);
            LogWL.e(TAG, tag.getInterneId()+" saved");
        }
    });
}


这是我的片段,称为BuchungstagFragment

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

    private BuchungstagSimpleCursorAdapter buchungstagCursorAdapter;
    private Anwender anwender;
    private Date syncDatum = Date.getInstance(1970, 1, 1);
    private OnItemSelectedListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.zeiterf_buchungstag_fragment, container);
        return v;
    }


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

        buchungstagCursorAdapter = new BuchungstagSimpleCursorAdapter(getActivity());
        setListAdapter(buchungstagCursorAdapter);

        getLoaderManager().initLoader(0, null, this);
    }


    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        this.anwender = (Anwender) args.get("anwender");
        this.syncDatum = (Date) args.get("syncDatum");
    }

    private static final String[] COLUMNS = new String[] { BuchungstagTable._ID, BuchungstagTable.BUCHUNGSTAG_ID,
            BuchungstagTable.FRUEH, BuchungstagTable.MITTAG, BuchungstagTable.ABEND, BuchungstagTable.NEBENKOSTEN,
            BuchungstagTable.NEBENKOSTEN_KOMMENTAR, BuchungstagTable.PARKGEBUEHREN,
            BuchungstagTable.PARKGEBUEHREN_KOMMENTAR, BuchungstagTable.SYNC_DATUM, BuchungstagTable.AEND_DATUM,
            BuchungstagTable.UEB_DATUM, BuchungstagTable.DATUM, BuchungstagTable.ANWENDER_ID };

    /*
     * (non-Javadoc)
     *
     * @see android.app.LoaderManager.LoaderCallbacks#onCreateLoader(int,
     * android.os.Bundle)
     */
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri baseUri = BuchungstagTable.CONTENT_URI;
        return new CursorLoader(getActivity(), baseUri, COLUMNS, BuchungstagTable.SYNC_DATUM + " = ?",
                new String[] { syncDatum.toString() }, null);
    }


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


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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnItemSelectedListener) {
            listener = (OnItemSelectedListener) activity;
        } else {
            throw new ClassCastException(activity.toString()
                    + " muss BuchungstagFragment.OnItemSelectedListener implementieren!");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        TBuchungstag tag = (TBuchungstag) buchungstagCursorAdapter.getItem(position);
        Bundle tagBundle = new Bundle();
        tagBundle.putSerializable("buchungstag", tag);
        listener.onBuchungstagItemSelected(tagBundle);
        getLoaderManager().restartLoader(0, null, this);
    }

    public interface OnItemSelectedListener {
        public void onBuchungstagItemSelected(Bundle tagBundle);
    }

}


这是我的BuchungstagSimpleCursorAdapter

public class BuchungstagSimpleCursorAdapter extends SimpleCursorAdapter {

    private static String[] COLUMNS = { BuchungstagTable.DATUM };

    private static int[] guifelder = { R.id.zeit_buchungstagTagLabel, R.id.zeit_buchungstagDatumLabel };

    private final LayoutInflater inflater;
    private Context context;

    public BuchungstagSimpleCursorAdapter(Context context) {
        super(context, R.layout.zeiterf_buchungstag, null, COLUMNS, guifelder, 0);
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.SimpleCursorAdapter#bindView(android.view.View,
     * android.content.Context, android.database.Cursor)
     */
    @Override
    public void bindView(View view, Context context, Cursor cur) {
        Date d = new Date(cur.getLong(cur.getColumnIndex(BuchungstagTable.DATUM)));
        String day = d.format("EEEE");
        String date = d.format("dd.MM.yyyy");

        TextView tag = (TextView) view.findViewById(R.id.zeit_buchungstagTagLabel);
        tag.setHintTextColor(view.getResources().getColor(R.color.buchungstagTagColor));
        tag.setText(day);

        TextView datum = (TextView) view.findViewById(R.id.zeit_buchungstagDatumLabel);
        datum.setText(date);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = inflater.inflate(R.layout.zeiterf_buchungstag, parent);
        return v;
    }
}


我的ContentProvider方法的getContext().getContentResolver().notifyChange()末尾有一个insert()。有人可以向我解释为什么这没有按预期进行吗?

最佳答案

我发现了我的错误。在我的onCreateLoader()方法中,我传递了一个选择参数,该参数防止光标在数据库中找到任何合适的项目。在删除参数之后,加载程序将按预期工作。

关于android - 如何使用LoaderManager和SimpleCursorAdapter填充ListFragment?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18381150/

10-10 09:25