将listview项从sqlite数据库绑定到cardview

时间:2016-10-25 16:51:26

标签: android listview android-sqlite android-cardview

有没有办法让每个列表项都显示在cardview上?这是我到目前为止尝试的,当我调试时我得到了列表项但我不能让它们在cardview上显示: 这是我的适配器代码:

    public class Item_adapter extends BaseAdapter {
    Context context;
   DBHelper mDbHelper = new DBHelper(context);
    LayoutInflater mInflater;
    DBHelper helper = new DBHelper(context);
List<ItemsHolder> list ;
    ViewHolder viewHolder;



    public Item_adapter(Context context, List list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.items_list,parent,false);

        viewHolder = new ViewHolder();

        viewHolder.ItemName = (TextView) convertView.findViewById(R.id.Item_name);
        viewHolder.ItemDescrip = (TextView) convertView.findViewById(R.id.Item_Description);
        convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }


        return convertView;
    }
    private static class ViewHolder {

        TextView ItemName;
        TextView ItemDescrip;
    }
}

这就是我在主要活动上称之为的地方:

mydb = new DBHelper(this);

listlist = mydb.getAllItems();

// ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array_list);     adapter = new Item_adapter(getApplicationContext(),listlist);

obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(adapter);
obj.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        ItemsHolder itemsHolder = (ItemsHolder)arg0.getItemAtPosition(position);
        int id_To_Search = itemsHolder.Id;

        Bundle dataBundle = new Bundle();
        dataBundle.putInt("id", id_To_Search);

        Intent intent = new Intent(getApplicationContext(), DsiplayItems.class);

        intent.putExtras(dataBundle);
        startActivity(intent);
    }
});

2 个答案:

答案 0 :(得分:1)

使用CursorAdapter。它旨在将数据库行绑定到ListView行。

此外,您将使用CursorLoader加载和管理活动中的光标。

答案 1 :(得分:0)

在您的类中,您应该引用listView并创建一个CursorAdapter类型字段,以便稍后在代码中引用它们。 您的CursorAdapter类应如下所示。

public class CursorListAdapter extends CursorAdapter {

    public CursorNoticeListAdapter(Context context) {
        super(context, null, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);

        // generate your view here and use viewholder pattern

        return view;
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // here you have viewHolder and the cursor from the database, now just bind them to views
        ViewHolder viewHolder = (ViewHolder) view.getTag();

    }

    public static class ViewHolder {
        // your viewHolder class
        public ViewHolder() {
        }
    }
}

然后查询数据库,实现此LoaderManager.LoaderCallbacks<Cursor>。在实现它时,您必须实现以下方法并在构造函数中初始化加载器。

public <Your-Class> () {
    getLoaderManager().initLoader(LOADER_ID, null, this);
}


@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // this is where you query the database, which shoild give you back a cursor
    // now just create the CursorLoader and return it 
    return new CursorLoader(getActivity(), URI, PROJECTION, selection, selectionArgs, sortOrder);
}


@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    cursorAdapter.swapCursor(data);
}


@Override
public void onLoaderReset(Loader<Cursor> loader) {
    cursorAdapter.swapCursor(null);
}

希望你能得到答案