代码内容来自于udicity
一般比较普遍的做法是,在帮助类中实现数据的查询,将查询的数据以集合的方式传入recyclerview的构造器中进行显示,此外还有一种方式就是,将查询到的cursor传递到recyclerview的构造器中,在recyclerview的onBindView方法中利用position进行查询后再进行展示,如下:
将查询到的cursor传递到recyclerview的构造器中:
// Get all guest info from the database and save in a cursor Cursor cursor = getAllGuests(); // Create an adapter for that cursor to display the data mAdapter = new GuestListAdapter(this, cursor);
在recyclerview的onBindView方法中利用position进行查询后再进行展示
@Override public void onBindViewHolder(GuestViewHolder holder, int position) { // Move the mCursor to the position of the item to be displayed if (!mCursor.moveToPosition(position)) return; // bail if returned null // Update the view holder with the information needed to display String name = mCursor.getString(mCursor.getColumnIndex(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME)); int partySize = mCursor.getInt(mCursor.getColumnIndex(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE)); // Display the guest name holder.nameTextView.setText(name); // Display the party count holder.partySizeTextView.setText(String.valueOf(partySize)); }
这种情况下,当数据发生改变更新数据时,应该在adapter中新建一个替换cursor的方法,如下:
public void swapCursor(Cursor newCursor){ // Inside, check if the current cursor is not null, and close it if so if(mCursor != null){ mCursor.close(); } // Update the local mCursor to be equal to newCursor mCursor = newCursor; // Check if the newCursor is not null, and call this.notifyDataSetChanged() if so if(newCursor != null){ this.notifyDataSetChanged(); } }
在mainActivity中调用它如下:
// call mAdapter.swapCursor to update the cursor by passing in getAllGuests() mAdapter.swapCursor(getAllGuests()); // To make the UI look nice, call .getText().clear() on both EditTexts, also call clearFocus() on mNewPartySizeEditText mNewGuestNameEditText.getText().clear(); mNewPartySizeEditText.getText().clear();
使用左滑或者右滑进行recyclerview的删除,删除数据库中的item,以该位置的id为唯一标识字段,但是id又不想在UI界面上展示,所以将id存储在recyclerview中的itemview的setTag中,如下:
在recyclerview中:
@Override public void onBindViewHolder(GuestViewHolder holder, int position) { // Move the mCursor to the position of the item to be displayed if (!mCursor.moveToPosition(position)) return; // bail if returned null // Update the view holder with the information needed to display String name = mCursor.getString(mCursor.getColumnIndex(WaitlistEntry.COLUMN_GUEST_NAME)); int partySize = mCursor.getInt(mCursor.getColumnIndex(WaitlistEntry.COLUMN_PARTY_SIZE)); // Retrieve the id from the cursor and long id = mCursor.getLong(mCursor.getColumnIndex(WaitlistEntry._ID)); // Display the guest name holder.nameTextView.setText(name); // Display the party count holder.partySizeTextView.setText(String.valueOf(partySize)); // Set the tag of the itemview in the holder to the id holder.itemView.setTag(id); }
在MainActivity中的onCreate方法中:
// Create a new ItemTouchHelper with a SimpleCallback that handles both LEFT and RIGHT swipe directions new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { @Override // Override onMove and simply return false inside public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } // Override onSwiped @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { // Inside, get the viewHolder's itemView's tag and store in a long variable id long id = (long) viewHolder.itemView.getTag(); // call removeGuest and pass through that id removeGuest(id); // call swapCursor on mAdapter passing in getAllGuests() as the argument mAdapter.swapCursor(getAllGuests()); } // attach the ItemTouchHelper to the waitlistRecyclerView }).attachToRecyclerView(waitlistRecyclerView);