所以我试图删除十字按钮上的项目。现在,它已从RecyclerView中删除,但是对于SQLite却很疯狂。如果我删除一项,那一切都很好。但是,当我删除几个项目时,正如我所说的,它会变得疯狂,并导致删除的项目无处不在而不删除任何内容。 这是我的DataAdapted.java
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<Booking> bookings;
private String time;
DBHelper dbHelper;
SQLiteDatabase dataBase;
DataAdapter(Context context, List<Booking> bookings) {
this.bookings = bookings;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public DataAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DataAdapter.ViewHolder holder, int position) {
Booking booking = bookings.get(position);
holder.nameView.setText(booking.getName());
time = booking.getHours() + ":" + booking.getMinutes();
holder.timeView.setText(time);
}
@Override
public int getItemCount() {
return bookings.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView nameView;
TextView timeView;
ImageView crossImage;
ViewHolder(View view){
super(view);
timeView = view.findViewById(R.id.time_view);
nameView = view.findViewById(R.id.name_view);
crossImage = view.findViewById(R.id.crossImage);
crossImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeAt(getAdapterPosition());
}
});
}
}
public void removeAt(int position) {
bookings.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, bookings.size());
DBHelper.delete(position);
}
}
DBHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "bookingsDb";
public static final String TABLE_BOOKINGS = "bookings";
public static final String KEY_ID = "_id";
public static final String KEY_NUMBER = "_numbers";
public static final String KEY_NAME = "_names";
public static final String KEY_HOURS = "_hours";
public static final String KEY_MINUTES = "_minutes";
public static final String KEY_PHONE= "_phones";
Context myContext;
static DBHelper dbHelper;
static SQLiteDatabase database;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_BOOKINGS + "("
+ KEY_ID + " integer primary key,"
+ KEY_NUMBER + " integer,"
+ KEY_NAME + " text,"
+ KEY_HOURS + " text,"
+ KEY_MINUTES + " text,"
+ KEY_PHONE + " text" + ")");
dbHelper = new DBHelper(myContext);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_BOOKINGS);
onCreate(db);
}
public static void delete(int id){
database = dbHelper.getWritableDatabase();
database.delete(TABLE_BOOKINGS, KEY_ID + "=" + id, null);
}
}
还有我的TableActivity.java(如果需要)
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class TableActivity extends AppCompatActivity {
int currentTable;
Button createNewBooking;
List<Booking> bookings;
TextView name;
TextView time;
DataAdapter adapter;
RecyclerView recyclerView;
DBHelper dbHelper;
View.OnClickListener createNewBookingListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TableActivity.this, BookingActivity.class);
startActivity(intent);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
currentTable = MainActivity.getCurrentTable();
createNewBooking = findViewById(R.id.createNewBooking);
createNewBooking.setOnClickListener(createNewBookingListener);
bookings = new ArrayList<>();
dbHelper = new DBHelper(this);
setInitialData();
recyclerView = findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new DataAdapter(this, bookings);
recyclerView.setAdapter(adapter);
}
private void setInitialData() {
SQLiteDatabase database = dbHelper.getWritableDatabase();
Cursor cursor = database.query(DBHelper.TABLE_BOOKINGS, null, null, null, null, null, null);
if (cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(DBHelper.KEY_NAME);
int hoursIndex = cursor.getColumnIndex(DBHelper.KEY_HOURS);
int minutesIndex = cursor.getColumnIndex(DBHelper.KEY_MINUTES);
int phoneIndex = cursor.getColumnIndex(DBHelper.KEY_PHONE);
do {
bookings.add(new Booking(
currentTable,
cursor.getString(nameIndex),
cursor.getString(hoursIndex),
cursor.getString(minutesIndex),
cursor.getString(phoneIndex)
));
} while (cursor.moveToNext());
} else {
Toast.makeText(this, "Броней нет", Toast.LENGTH_SHORT).show();
}
cursor.close();
dbHelper.close();
}
}
有什么想法吗?
答案 0 :(得分:0)
您要提供删除位置,无需ID与每个适配器位置相同。
试试这个:
crossImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Booking booking = bookings.get(getAdapterPosition());
datatype id = booking.getId();
removeAt(getAdapterPosition(),id);
}
});
public void removeAt(int position, dataType id) {
bookings.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, bookings.size());
DBHelper.delete(id);
}