2

I have a layout with a ListFragment and a Button. When I click the button, a new Buchungstag object is created and stored in the database. Now I supposed the LoaderManager to notice this change and create a new cursor. Then the ListFragment should display the new object, but nothing happens. Can someone tell me where I am wrong?

This is the onCreate() of my activity:

@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");
        }
    });
}

This is my Fragment called 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);
    }

}

This is my 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;
    }
}

My ContentProvider has a getContext().getContentResolver().notifyChange() at the end of its insert() method. Can someone explain to me why this is not working as expected?

4

1 回答 1

1

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

于 2013-08-23T08:49:24.007 回答