从SQLite数据库获取数据到Custom Card GridView(图书馆:Gabrielemariotti)

时间:2015-05-09 13:02:00

标签: android sqlite gridview

我正在使用此卡片库https://github.com/gabrielemariotti/cardslib。我已经实现了自定义cardgridcursoradapter并在此类的构造函数中传递了游标。 这是我的自定义cardgridcursoradapter类。

class CustCursorAdapter extends CardGridCursorAdapter{


    public CustCursorAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }

    @Override
    protected Card getCardFromCursor(Cursor cursor) {

        MyCard card = new MyCard(super.getContext());

        return card;
    }

}

这不显示卡网格视图。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

嘿,我的朋友你所做的一切都是对的,但正如你所说的那样是自定义卡网格视图,你需要用你的卡返回一个视图。 这意味着您的适配器类需要覆盖newview和bindview方法。像这样。

class CustCursorAdapter extends CardGridCursorAdapter{


public CustCursorAdapter(Context context, Cursor c, boolean autoRequery) {
    super(context, c, autoRequery);
}

@Override
protected Card getCardFromCursor(Cursor cursor) {

    MyCard card = new MyCard(super.getContext());

    return card;
 }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.card_grid_item, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        TextView id = (TextView) view.findViewById(R.id.id);
        TextView name = (TextView) view.findViewById(R.id.name);
        TextView username = (TextView) view.findViewById(R.id.username);
        TextView password = (TextView) view.findViewById(R.id.password);
        Integer cursor_id= cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
        String cursor_name=cursor.getString(cursor.getColumnIndexOrThrow("Name"));
        String cursor_username=cursor.getString(cursor.getColumnIndexOrThrow("Username"));
        String cursor_password=cursor.getString(cursor.getColumnIndexOrThrow("Password"));
        id.setText(String.valueOf(cursor_id));
        name.setText(cursor_name);
        username.setText(cursor_username);
        password.setText(cursor_password);
    }
}

像这样更新了你的适配器子类。 (请记住更新mycard类布局和java文件。)