我正在使用幻灯片从recyclerview中删除但是当getAdapterPosition()与sql表的row_id不匹配时会出现问题。如何从适配器获取id以及如何在添加数据时更新recyclerview(在重新打开应用程序或更改方向之前它不显示新数据)?
公共类MainActivity扩展了AppCompatActivity {
RecyclerView mRecyclerView;
MyAdapter myAdapter;
ImageButton floatButton;
DbHelper dbHelper;
ArrayList<Information> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatButton = (ImageButton) findViewById(R.id.imageButton);
floatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddDetailPage.class);
MainActivity.this.startActivity(intent);
}
});
dbHelper = new DbHelper(this);
data = new ArrayList<>();
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
myAdapter = new MyAdapter(this, showData());
mRecyclerView.setAdapter(myAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
ItemTouchHelper helper = new ItemTouchHelper(createHelperCallback());
helper.attachToRecyclerView(mRecyclerView);
}
private ItemTouchHelper.Callback createHelperCallback() {
ItemTouchHelper.SimpleCallback simpleItemCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
deleteItem(viewHolder.getAdapterPosition());
}
};
return simpleItemCallback;
}
public ArrayList<Information> showData() {
Cursor res = dbHelper.showData();
while (res.moveToNext()) {
Information information = new Information();
information.ID = res.getString(0);
information.first = res.getString(0);
information.second = res.getString(2);
information.detail = res.getString(3);
data.add(information);
}
return data;
}
public void deleteItem(int position){
DbHelper dbHelper = new DbHelper(this);
dbHelper.delete(position);
myAdapter.notifyItemRemoved(position);
myAdapter.delete(position);
}
}
公共类MyAdapter扩展了RecyclerView.Adapter {
private MainActivity mainactivity;
private ArrayList<Information> data;
private LayoutInflater layoutInflater;
public MyAdapter(MainActivity mainActivity, ArrayList<Information> information) {
this.mainactivity = mainActivity;
this.data = information;
layoutInflater = LayoutInflater.from(mainActivity);
notifyDataSetChanged();
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view, data);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
holder.first.setText(data.get(position).first);
holder.second.setText(data.get(position).second);
holder.detail.setText(data.get(position).detail);
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView first, second, detail;
ArrayList<Information> info;
public ViewHolder(View itemView, ArrayList<Information> data) {
super(itemView);
this.info = data;
first = (TextView) itemView.findViewById(R.id.firstText);
second = (TextView) itemView.findViewById(R.id.secondText);
detail = (TextView) itemView.findViewById(R.id.detail);
}
}
public void delete(int x){
data.remove(x);
notifyDataSetChanged();
}
}
public class DbHelper扩展了SQLiteOpenHelper {
public static final String DATABASE_NAME = "students.db";
public static final String TABLE_NAME = "student_table";
public static final String ID = "_id";
public static final String FIRST_NAME = "first_name";
public static final String SECOND_NAME = "second_name";
public static final String DETAIL = "detail";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + FIRST_NAME + " TEXT ," + SECOND_NAME + " TEXT ," + DETAIL + " TEXT );");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String first, String second, String detail) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(FIRST_NAME, first);
contentValues.put(SECOND_NAME, second);
contentValues.put(DETAIL, detail);
long x = db.insert(TABLE_NAME, null, contentValues);
if (x == -1) {
return false;
} else {
return true;
}
}
public Cursor showData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return res;
}
public void delete(int x) {
int y = x + 1;
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, ID + "=" + y, null);
}
}
答案 0 :(得分:0)
在适配器中创建此方法以获取该特定对象的get id,并将id传递给删除DbHelper类的函数。
int getId(int position){
return data.get(position).ID;
}
避免直接访问班级成员。而不是你可以使用getter和setter方法。
要在添加新数据后更新recyclerview,您可以在适配器的相应insert方法中调用notifyDataSetChanged()。
修改强>
要在您的回收站视图中添加新元素,请在适配器中添加此方法
void addItem(Informationn infoObj){
data.add(infoObj);
notifyDataSetChanged();
}
使用您的适配器对象调用此方法并将新项目作为参数传递,您的recyclerview将使用新项目进行更新
答案 1 :(得分:0)
如果像这样在yr适配器中覆盖了+--------+-----------+---------------+-------+---------------+
| DATE | STORE_NUM | CITY | VALUE | DISTANCE |
+--------+-----------+---------------+-------+---------------+
| 1/1/20 | 1 | Atlanta | 10 | 8.245620101 |
| 1/2/20 | 1 | Atlanta | 15 | 8.245620101 |
| 1/3/20 | 1 | Atlanta | 13 | 8.245620101 |
| 1/1/20 | 2 | Atlantic City | 5 | 105.009087658 |
| 1/2/20 | 2 | Atlantic City | 7 | 105.009087658 |
| 1/3/20 | 2 | Atlantic City | 10 | 105.009087658 |
| 1/1/20 | 3 | Denver | 11 | 39.851626235 |
| 1/2/20 | 3 | Denver | 12 | 39.851626235 |
| 1/3/20 | 3 | Denver | 14 | 39.851626235 |
| 1/1/20 | 2 | Philadelphia | 20 | 9.886319193 |
| 1/2/20 | 2 | Philadelphia | 25 | 9.886319193 |
| 1/3/20 | 2 | Philadelphia | 22 | 9.886319193 |
+--------+-----------+---------------+-------+---------------+
方法,则使用
getItemId
这样,您可以使用getItemId方法获取商品位置和ID