我应该使用额外的arraylist进行高效的数据操作,同时将Sqlite db与recyclerviews一起使用吗?



我希望有直截了当的问题。

1(我设法从Sqlite db获取数据并在回收器视图上显示它们。问题是,例如,当我单击回收器视图项目并执行一些操作(例如复制内容或更新(时,是否最好使用 arraylist 并在应用程序加载时先获取数据,然后对此 arraylist 元素进行操作(然后最终通知 db(?

2(如果在再次单击回收器视图项目时不需要在onContextItemSelected((操作上添加额外的arraylist,那么我在选择元素及其值时遇到了一些麻烦。

public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.idshare :
//implicit intent
shareImplicitIntent();
return true;

......

对于 shareImplicitIntent(( 方法

private void shareImplicitIntent() {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Cursor cursor=WordListOpenHelper.mReadableDB.rawQuery("SELECT * FROM 
diary", null);
cursor.moveToPosition(XYZ);
Entry_Model entry_model= new Entry_Model();
entry_model.setmEntry(cursor.getString(cursor.getColumnIndex(WordListOpenHelper.KEY_ENTRY)));
String title = entry_model.getmEntry(); ......

基本上使用光标并在XYZ位置获取光标的标题。

但是我该如何选择那个XYZ位置呢?

工作时间在上面,但找不到线索。请帮助我。多谢

我自己回答我的问题,很快没有,例如从用户那里获取输入并将它们放入数组列表中,然后在 arraylist 上执行数据库操作不是很有用也没有必要。(然而,如果您的数据库计划只包含少量条目,尽管您可以使用 arraylist/linkedlist 在回收器视图适配器上进行快速 CRUD 操作(。

对于问题的第二部分,例如,通过在视图持有人内部类的视图持有者构造函数中创建setonclicklistener,很容易复制单击的回收器视图元素的内容; (注意与示例中不同的是,如果您不打算与其他应用程序共享数据库中的数据,则不必使用 ContentResolver(

itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos =getAdapterPosition();
String entry = "";
String[] mProjection =
{ Contract.WordList.KEY_ENTRY,    // Contract class constant for the _ID column name  };


Cursor cursor = mContext.getContentResolver().query(Uri.parse(
queryUri), mProjection, null, null, sortOrder);
if (cursor != null) {
if (cursor.moveToPosition(pos)) {
int indexEntry = cursor.getColumnIndex(Contract.WordList.KEY_ENTRY);
entry = cursor.getString(indexEntry);

}
}

Toast.makeText(v.getContext(), "copied entry is " + entry, Toast.LENGTH_LONG).show();
return false;
}
});

相关内容

最新更新