Kore/app/src/main/java/org/xbmc/kore/ui/RecyclerViewCursorAdapter.java

93 lines
2.8 KiB
Java

/*
* Copyright 2018 Martijn Brekhof. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xbmc.kore.ui;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.View;
abstract public class RecyclerViewCursorAdapter extends RecyclerView.Adapter<RecyclerViewCursorAdapter.CursorViewHolder> {
private boolean dataValid;
private int rowIDColumn;
private Cursor cursor;
@Override
public void onBindViewHolder(CursorViewHolder holder, int position) {
if (!dataValid) {
throw new IllegalStateException("Cannot bind viewholder when cursor is in invalid state.");
}
if (!cursor.moveToPosition(position)) {
throw new IllegalStateException("Could not move cursor to position " + position + " when trying to bind viewholder");
}
holder.bindView(cursor);
}
@Override
public int getItemCount() {
if (dataValid) {
return cursor.getCount();
} else {
return 0;
}
}
@Override
public long getItemId(int position) {
if (!dataValid) {
throw new IllegalStateException("Cursor is in an invalid state.");
}
if (!cursor.moveToPosition(position)) {
throw new IllegalStateException("Could not move cursor to position " + position);
}
return cursor.getLong(rowIDColumn);
}
public void swapCursor(Cursor newCursor) {
if (newCursor == cursor) {
return;
}
if (newCursor != null) {
cursor = newCursor;
rowIDColumn = cursor.getColumnIndexOrThrow("_id");
dataValid = true;
notifyDataSetChanged();
} else {
notifyItemRangeRemoved(0, getItemCount());
cursor = null;
rowIDColumn = -1;
dataValid = false;
}
}
abstract public static class CursorViewHolder extends RecyclerView.ViewHolder {
public CursorViewHolder(View itemView) {
super(itemView);
itemView.setTag(this);
}
/**
* Called to update the content of {@link RecyclerView.ViewHolder#itemView} this holder holds.
*/
abstract public void bindView(Cursor cursor);
}
}