Java Android-光标适配器和回收器视图存在问题
因此,我正确地为我的Java Android-光标适配器和回收器视图存在问题,java,android,android-5.0-lollipop,material-design,Java,Android,Android 5.0 Lollipop,Material Design,因此,我正确地为我的RecyclerView实现了CursorAdapter(afaik),但我遇到了一些奇怪的错误。下面是代码片段。非常感谢您的帮助,谢谢 以下是日志: 这是游标或RecyclerAdapter: public abstract class CursorRecyclerAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { protected
RecyclerView
实现了CursorAdapter
(afaik),但我遇到了一些奇怪的错误。下面是代码片段。非常感谢您的帮助,谢谢
以下是日志:
这是游标或RecyclerAdapter
:
public abstract class CursorRecyclerAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
protected boolean mDataValid;
protected Cursor mCursor;
protected int mRowIDColumn;
public CursorRecyclerAdapter(Cursor c) {
init(c);
}
void init(Cursor c) {
boolean cursorPresent = c != null;
mCursor = c;
mDataValid = cursorPresent;
mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
setHasStableIds(true);
}
@Override
public final void onBindViewHolder (VH holder, int position) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
onBindViewHolder(holder, mCursor);
}
public abstract void onBindViewHolder(VH holder, Cursor cursor);
public Cursor getCursor() {
return mCursor;
}
@Override
public int getItemCount () {
if (mDataValid && mCursor != null) {
return mCursor.getCount();
} else {
return 0;
}
}
@Override
public long getItemId (int position) {
if(hasStableIds() && mDataValid && mCursor != null){
if (mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIDColumn);
} else {
return RecyclerView.NO_ID;
}
} else {
return RecyclerView.NO_ID;
}
}
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*
* @param cursor The new cursor to be used
*/
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
/**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*
* @param newCursor The new cursor to be used.
* @return Returns the previously set Cursor, or null if there wasa not one.
* If the given new Cursor is the same instance is the previously set
* Cursor, null is also returned.
*/
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
Cursor oldCursor = mCursor;
mCursor = newCursor;
if (newCursor != null) {
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -1;
mDataValid = false;
// notify the observers about the lack of a data set
notifyItemRangeRemoved(0, getItemCount());
}
return oldCursor;
}
/**
* <p>Converts the cursor into a CharSequence. Subclasses should override this
* method to convert their results. The default implementation returns an
* empty String for null values or the default String representation of
* the value.</p>
*
* @param cursor the cursor to convert to a CharSequence
* @return a CharSequence representing the value
*/
public CharSequence convertToString(Cursor cursor) {
return cursor == null ? "" : cursor.toString();
}
}
公共抽象类CursorRecyclerAdapter扩展了RecyclerView.Adapter{
受保护的布尔mDataValid;
保护光标mCursor;
受保护的int-mRowIDColumn;
公共游标RecyclerAdapter(游标c){
init(c);
}
void init(光标c){
布尔cursorPresent=c!=null;
mCursor=c;
mDataValid=cursorPresent;
mRowIDColumn=cursorPresent?c.getColumnIndexOrThrow(“\u id”):-1;
setHasStableIds(true);
}
@凌驾
BindViewHolder上的公共最终无效(VH holder,内部位置){
如果(!mDataValid){
抛出新的IllegalStateException(“仅当光标有效时才应调用它”);
}
如果(!mCursor.moveToPosition(位置)){
抛出新的IllegalStateException(“无法将光标移动到位置”+位置);
}
onBindViewHolder(holder,mCursor);
}
BindViewHolder上的公共摘要无效(VH holder,光标);
公共游标getCursor(){
返回mCursor;
}
@凌驾
public int getItemCount(){
if(mDataValid&&mCursor!=null){
返回mCursor.getCount();
}否则{
返回0;
}
}
@凌驾
公共长getItemId(int位置){
if(hasStableIds()&&mDataValid&&mCursor!=null){
if(mCursor.moveToPosition(位置)){
返回mCursor.getLong(mRowIDColumn);
}否则{
返回RecyclerView.NO\u ID;
}
}否则{
返回RecyclerView.NO\u ID;
}
}
/**
*将基础游标更改为新游标。如果存在现有游标,则它将
*关闭。
*
*@param cursor要使用的新光标
*/
公共void changeCursor(游标){
游标old=swapCursor(游标);
如果(旧!=null){
old.close();
}
}
/**
*交换新光标,返回旧光标。与
*{@link#changeCursor(Cursor)},返回的旧游标不是
*关闭。
*
*@param newCursor要使用的新光标。
*@return返回以前设置的游标,如果没有游标,则返回null。
*如果给定的新光标是相同的,则实例是先前设置的
*游标,也将返回null。
*/
公共游标交换游标(游标新建游标){
if(newCursor==mCursor){
返回null;
}
游标oldCursor=mCursor;
mCursor=newCursor;
if(newCursor!=null){
mRowIDColumn=newCursor.getColumnIndexOrThrow(“\u id”);
mDataValid=true;
//将新光标通知观察者
notifyDataSetChanged();
}否则{
mRowIDColumn=-1;
mDataValid=false;
//通知观察者缺少数据集
notifyItemRangeRemoved(0,getItemCount());
}
返回光标;
}
/**
*将游标转换为CharSequence。子类应重写此操作
*方法转换其结果。默认实现返回
*空字符串表示空值或的默认字符串表示形式
*价值
*
*@param cursor要转换为字符序列的光标
*@返回表示该值的CharSequence
*/
公共字符序列convertToString(游标){
返回游标==null?”:cursor.toString();
}
}
以下是Recycler视图适配器:
public class RVAdapter extends CursorRecyclerAdapter<RVAdapter.MyViewHolder> {
private LayoutInflater inflater;
CursorAdapter mCursorAdapter;
Context mContext;
public ManagerDatabaseAdapter managerDatabaseAdapter;
public RVAdapter (Context context, Cursor cursor) {
super(cursor);
managerDatabaseAdapter = new ManagerDatabaseAdapter(context);
Message.message(context, cursor.toString());
mContext = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Message.message(mContext, "Holder Called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, Cursor cursor) {
cursor.moveToFirst();
Message.message(mContext, "Binder Called");
int banana = cursor.getColumnIndex("Codes");
Message.message(mContext, banana);
super.onBindViewHolder(holder, banana);
}
@Override
public int getItemCount() {
return super.getItemCount();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title;
MyViewHolder(View itemView) {
super(itemView);
Title = (TextView) itemView.findViewById(R.id.Row_Header);
//Body = (TextView) itemView.findViewById(R.id.Row_Footer);
}
}
}
公共类RVAdapter扩展了CursorRecyclerAdapter{
私人充气机;
游标适配器mCursorAdapter;
语境;
公共管理器DatabaseAdapter管理器DatabaseAdapter;
公共RVAdapter(上下文、游标){
超级(光标);
managerDatabaseAdapter=新的managerDatabaseAdapter(上下文);
Message.Message(上下文,cursor.toString());
mContext=上下文;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
Message.Message(mContext,“呼叫持有者”);
View=LayoutInflater.from(parent.getContext()).flate(R.layout.custom_行,parent,false);
MyViewHolder=新的MyViewHolder(视图);
报税表持有人;
}
@凌驾
BindViewHolder上的公共无效(MyViewHolder,光标){
cursor.moveToFirst();
Message.Message(mContext,称为Binder);
int=cursor.getColumnIndex(“代码”);
Message.Message(mContext,banana);
super.onBindViewHolder(支架,香蕉);
}
@凌驾
public int getItemCount(){
返回super.getItemCount();
}
类MyViewHolder扩展了RecyclerView.ViewHolder{
文本视图标题;
MyViewHolder(查看项目视图){
超级(项目视图);
Title=(TextView)itemView.findViewById(R.id.Row\u标题);
//Body=(TextView)itemView.findViewById(R.id.Row\u Footer);
}
}
}
最后,这里是MainActivity.java:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.nick.mowen.receiptmanager.LOCATION";
public RecyclerView RV;
private RVAdapter adapter;
ManagerDatabaseAdapter managerDatabaseAdapter;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
setContentView(R.layout.activity_main);
adapter = new RVAdapter(this, managerDatabaseAdapter.getTheCursor());
RV = (RecyclerView) findViewById(R.id.mainV);
layoutManager = new LinearLayoutManager(this);
RV.setLayoutManager(layoutManager);
RV.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// getMenuInflater().inflate(R.menu.menu_main, li)
return true;
}
public static void getData() {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra(EXTRA_MESSAGE, true);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
return true;
}
return super.onOptionsItemSelected(item);
}
/*@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView userText= (TextView) view;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}*/
public void addInstance(View view) {
Intent intent = new Intent(this, LocationAdder.class);
intent.putExtra(EXTRA_MESSAGE, true);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
}
}
public类MainActivity扩展了AppCompatActivity{
公共静态最终字符串EXTRA_MESSAGE=“com.nick.mowen.receiptmanager.LOCATION”;
公共回收站;
专用适配器;
ManagerDatabaseAdapter ManagerDatabaseAdapter;
RecyclerView.LayoutManager LayoutManager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
managerDatabaseAdapter=新的managerDatabaseAdapter(此);
setContentView(R.layout.activity\m