我之前提出了一个关于如何从listview交换到recyclerview的问题。但是我注意到,在我的更改中,我的更新和删除没有得到保存。根据我的理解,这与更新内容所需的游标有关。然而,当前我遇到了以下错误
showAdapter = new ShowsAdapter(this, shows); // the error
错误:(68,50)错误:不兼容的类型:List无法转换为Cursor
private List<Show> shows;
private ShowsAdapter showAdapter;
private RecyclerView recyclerView;
private DataSource datasource;
public static final String INTENT_DETAIL_ROW_NUMBER = "Row number";
public static final String INTENT_DETAIL_REMINDER_TEXT = "Reminder text";
public static final int REQUESTCODE = 2;
public static final String EXTRA_SHOW_ID = "extraShowId";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
datasource = new DataSource(this);
datasource.open();
recyclerView = (RecyclerView) findViewById(R.id.main_list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
//recyclerView.setAdapter(showAdapter);
updateUI();
//registerForContextMenu(recyclerView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.taskView);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this, CreateShowActivity.class), 1);
}
});
}
private void updateUI() {
shows = datasource.getAllShows();
if (showAdapter == null) {
showAdapter = new ShowsAdapter(this, shows); // the error
recyclerView.setAdapter(showAdapter);
} else {
showAdapter.notifyDataSetChanged();
}
}
我的适配器:
private List<Show> mShows;
private Context mContext;
private Cursor mShowsCursor;
public void updateList(List<Show> newlist) {
// Set new updated list
mShows.clear();
mShows.addAll(newlist);
notifyDataSetChanged();
}
public ShowsAdapter(Context mContext, Cursor cursor) {
this.mContext = mContext;
this.mShowsCursor = cursor;
}
@Override
public ShowsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, null);
// Return a new holder instance
ShowsAdapter.ViewHolder viewHolder = new ShowsAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ShowsAdapter.ViewHolder holder, final int position) {
if (mShowsCursor != null && mShowsCursor.moveToPosition(position)) {
holder.textView.setText(mShowsCursor.getString(mShowsCursor.getColumnIndex(MySQLiteHelper.COLUMN_SHOW)));
}
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, EditShowActivity.class);
intent.putExtra(INTENT_DETAIL_ROW_NUMBER, position);
intent.putExtra(INTENT_DETAIL_REMINDER_TEXT, holder.textView.getText());
((MainActivity) mContext).startActivityForResult(intent, REQUESTCODE);
}
});
holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mShows.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mShows.size());
return true;
}
});