Search code examples
androidsqlitelistviewandroid-cursor

My listview doesn't load or show the cursor values


public class ListenFragment extends Fragment implements LoaderCallbacks<Cursor>{

private ListView eintragListe;
private DatabaseHandler dbHandler;
private EintragListAdapter adapter;
private ProgressDialog waitDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View root = inflater.inflate(R.layout.fragment_list, null);


    waitDialog = new ProgressDialog(getActivity());
    waitDialog.setTitle("warte1");
    waitDialog.setMessage("warte");
    waitDialog.setIndeterminate(true);
    waitDialog.show();

    eintragListe = (ListView) root.findViewById(R.id.entryListView);
    dbHandler = new DatabaseHandler(getActivity());
    adapter = new EintragListAdapter(getActivity(), null);
    eintragListe.setAdapter(adapter);

    eintragListe.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position,
                long id) {
            Cursor cursor = (Cursor) eintragListe.getItemAtPosition(position);

            int idIndex = cursor.getColumnIndex(DatabaseHandler.EINTRAG_ID);
            int contentIndex = cursor.getColumnIndex(DatabaseHandler.EINTRAG_CONTENT);

            String content = cursor.getString(contentIndex);
            long newId = cursor.getLong(idIndex);

            NotificationEntry entry = new NotificationEntry();
            entry.setId(newId);
            entry.setEintrag(content);

            //entrySelected(entry);

        }
    });

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


}



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

private static class EintragListAdapter extends CursorAdapter {

    public EintragListAdapter(Context context, Cursor c) {
        super(context, c);

    }

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

        int idIndex = cursor.getColumnIndex(DatabaseHandler.EINTRAG_ID);
        int contentIndex = cursor.getColumnIndex(DatabaseHandler.EINTRAG_CONTENT);

        TextView titleView = (TextView) view.findViewById(R.id.title);
        titleView.setText(cursor.getString(contentIndex));



    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.listitemview, parent, false);

        return retView;
    }

}

private static class EintragCursorLoader extends CursorLoader {

    public EintragCursorLoader(Context context) {
        super(context);
    }

    @Override
    protected Cursor onLoadInBackground() {
        DatabaseHandler dbHandler = new DatabaseHandler(getContext());
        Cursor eintragCursor = dbHandler.getAllEntryCursor();

        return eintragCursor;
    }

}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    EintragCursorLoader loader = new EintragCursorLoader(getActivity());
    return loader;
}

@Override
public void onLoadFinished(Loader<Cursor> laoder, Cursor cursor) {
    adapter.swapCursor(cursor);
    waitDialog.dismiss();

}

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

}
@Override
public void onDestroyView() {
    dbHandler.close();
    super.onDestroyView();
}

public void reload(){
    getLoaderManager().initLoader(0, null, this);
}

}

And hear is the getAllEntryCursor() method:

public Cursor getAllEntryCursor() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(EINTRAG_TABLE, null, null, null, null, null,
            null);
    return cursor;
}

Any idea why ListView doesn't show anything?? I had already checked, that the db has content. So the saveMethod works..


Solution

  • All that code you have in onCreate(), move it to onCreateView()