Search code examples
androidsimplecursoradapterandroid-cursorloader

Android SimpleCursorAdapter to CursorAdapter


I'm using SimpleCursorAdapter to manage my query to the database and then showing the results in a ListView.

Here is my code:

database.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor c = database.getData();
startManagingCursor(c);


String[] from = new String[]{Database._GROUP_NAME};
int[] to = new int[]{android.R.id.text1};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, query, from, to);

listContent.setAdapter(adapter);

database.close();

Although, at first look, it it working as expected, I believe that this should NOT be the way to proceed since there is CursorAdapter.

I know that this is a noob question but since just started now programming in Android I still don't know much.

How can I pass this from using SimpleCursorAdapter to CursorAdapter? Already searched on internet but wasn't able to understand how this can be done.

Not asking code just some directions.

Thanks

favolas

UPDATE FOLLOWING mainu COMMENT

database.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor c = database.getData();
MyAdapt cursorAdapter = new MyAdapt(this, query);


listContent.setAdapter(adapter);

database.close();

MyAdapt class:

public class MyAdapt extends CursorAdapter {

    private final LayoutInflater mInflater;

    public MyAdapt(Context context, Cursor c) {
        super(context, c);
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

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

        TextView groupName = (TextView) view.findViewById(android.R.id.text1);
        groupName.setText(cursor.getString(cursor
                .getColumnIndex(Database._GROUP_NAME)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final View view = mInflater.inflate(
                android.R.layout.simple_list_item_1, parent, false);
        return view;
    }
}

Is this the correct way?

favolas


Solution

  • SimpleCursorAdapter is the simplest form of Adapter you can use for a custom adapter. You should use SimpleCursorAdapter only, by when you dont need any customization.