【问题标题】:My listview doesn't load or show the cursor values我的列表视图没有加载或显示光标值
【发布时间】:2014-05-26 01:24:00
【问题描述】:
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);
}

}

并且听到的是getAllEntryCursor() 方法:

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

知道为什么 ListView 不显示任何内容吗? 我已经检查过数据库是否有内容。 所以 saveMethod 有效..

【问题讨论】:

    标签: android sqlite listview android-cursor


    【解决方案1】:

    您在 onCreate() 中的所有代码,将其移至 onCreateView()

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-10
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    相关资源
    最近更新 更多