当我在其中一个输入上点击删除时,它会删除所有记录而不是一个记录,我过去两天都试图解决这个问题而没有运气。
[什么应用程序看起来] [1]
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ArrayList<Item>list = new ArrayList<Item>();
private ItemAdapter adapter;
private RecyclerView recyclerView;
private AlertDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fetchRecords();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = getLayoutInflater();
View mView1 = inflater.inflate(R.layout.activity_add,null);
final EditText input_name = (EditText) mView1.findViewById(R.id.Name);
final EditText input_age = (EditText) mView1.findViewById(R.id.Age);
final EditText input_weight = (EditText) mView1.findViewById(R.id.Weight);
final EditText input_height = (EditText) mView1.findViewById(R.id.Height);
final EditText input_reach = (EditText) mView1.findViewById(R.id.Reach);
final Button btnSave = (Button) mView1.findViewById(R.id.btnSave);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(mView1).setTitle("Add new Record")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = input_name.getText().toString();
String age = input_age.getText().toString();
String weight = input_weight.getText().toString();
String height = input_height.getText().toString();
String reach = input_reach.getText().toString();
if (name.equals("") && age.equals("") && weight.equals("") && height.equals("")&& reach.equals("")){
Snackbar.make(view,"Field incomplete",Snackbar.LENGTH_SHORT).show();
}else {
Save(name,age,name,height,reach);
dialog.dismiss();
Snackbar.make(view,"Saving",Snackbar.LENGTH_SHORT).show();
}
}
});
dialog = builder.create();
dialog.show();
}
});
}
public void fetchRecords() {
recyclerView = (RecyclerView) findViewById(R.id.recycler);
adapter = new ItemAdapter(this,list);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
list.clear();
Functions functions = new Functions(MainActivity.this);
ArrayList<Item>data = functions.getAllRecords();
if (data.size()>0){
for (int i = 0; i<data.size(); i++){
int id = data.get(i).getId();
String namel = data.get(i).getName();
String agel = data.get(i).getAge();
String weightl = data.get(i).getWeight();
String heightl = data.get(i).getHeight();
String reachl = data.get(i).getReach();
Item item = new Item();
item.setId(id);
item.setName(namel);
item.setAge(agel);
item.setWeight(weightl);
item.setHeight(heightl);
item.setReach(reachl);
list.add(item);
}adapter.notifyDataSetChanged();
}else {
Toast.makeText(MainActivity.this, "No Records found.", Toast.LENGTH_SHORT).show();
}
}
private void Save(String name, String age, String weight, String height, String reach) {
Functions functions = new Functions(MainActivity.this);
Item item = new Item();
item.setName(name);
item.setAge(age);
item.setWeight(weight);
item.setHeight(height);
item.setReach(reach);
functions.Insert(item);
Toast.makeText(MainActivity.this, "Saved...", Toast.LENGTH_SHORT).show();
fetchRecords();
}
@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);
return true;
}
}
public class Functions extends SQLiteOpenHelper {
private static final String DB_NAME = "crud.db";
private static final int DB_VERSION = 1;
private Fighter fighter = new Fighter();
public Functions(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(fighter.CREATE_TABLE_PERSON);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + fighter.TABLE_FIGHTER);
}
public void Insert(Item item){
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(fighter.ID,item.getId());
contentValues.put(fighter.NAME,item.getName());
contentValues.put(fighter.AGE,item.getAge());
contentValues.put(fighter.WEIGHT,item.getWeight());
contentValues.put(fighter.HEIGHT,item.getHeight());
contentValues.put(fighter.REACH,item.getReach());
database.insert(fighter.TABLE_FIGHTER,null,contentValues);
}
public ArrayList<Item>getAllRecords(){
ArrayList<Item>list = new ArrayList<Item>();
SQLiteDatabase database = getReadableDatabase();
String sql = "SELECT * FROM " + fighter.TABLE_FIGHTER;
Cursor cursor = database.rawQuery(sql,null);
if (cursor.moveToFirst()){
do {
Item item = new Item();
item.setId(Integer.parseInt(cursor.getString(0)));
item.setName(cursor.getString(1));
item.setAge(cursor.getString(2));
item.setWeight(cursor.getString(3));
item.setHeight(cursor.getString(4));
item.setReach(cursor.getString(5));
list.add(item);
}while (cursor.moveToNext());
}
cursor.close();
database.close();
return list;
}
public Item getSingleItem(int id){
SQLiteDatabase database = getReadableDatabase();
String sql = "SELECT * FROM " + fighter.TABLE_FIGHTER + " WHERE " + fighter.ID + "=?";
Cursor cursor = database.rawQuery(sql,new String[]{String.valueOf(id)});
if (cursor != null)
cursor.moveToNext();
Item item = new Item();
item.setId(Integer.parseInt(cursor.getString(0)));
item.setName(cursor.getString(1));
item.setAge(cursor.getString(2));
item.setWeight(cursor.getString(3));
item.setHeight(cursor.getString(4));
item.setReach(cursor.getString(4));
cursor.close();
database.close();
return item;
}
public void DeleteItem(int id){
SQLiteDatabase database = getWritableDatabase();
database.delete(fighter.TABLE_FIGHTER,fighter.ID + "=?",new String[]{String.valueOf(id)});
database.close();
}
public void Update(Item item){
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(fighter.NAME,item.getName());
contentValues.put(fighter.AGE,item.getAge());
contentValues.put(fighter.WEIGHT,item.getWeight());
contentValues.put(fighter.HEIGHT,item.getHeight());
contentValues.put(fighter.REACH,item.getReach());
database.update(fighter.TABLE_FIGHTER,contentValues,fighter.ID + "=?",new String[]{String.valueOf(item.getId())});
}
}
公共阶级斗士{
public static final String TABLE_FIGHTER = "fighter";
public static final String ID = "ID";
public static final String NAME = "Name";
public static final String AGE = "Age";
public static final String WEIGHT = "Weight";
public static final String HEIGHT = "Height";
public static final String REACH = "Reach";
public static final String CREATE_TABLE_PERSON = "CREATE TABLE " + TABLE_FIGHTER + "("
+ ID + " INTEGER PRIMARY KEY,"
+ NAME + " TEXT,"
+ AGE + " TEXT,"
+ WEIGHT + " TEXT,"
+ HEIGHT + " TEXT"
+ REACH + " TEXT"+ ")";
}
公共类ItemAdapter扩展了RecyclerView.Adapter {
private Activity activity;
private ArrayList<Item>list;
private AlertDialog dialog;
public ItemAdapter(Activity activity, ArrayList<Item> list) {
this.activity = activity;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final Item item = list.get(position);
holder.name.setText("Name: " + item.getName());
holder.age.setText("Age: " + item.getAge());
holder.weight.setText("Weight: " + item.getWeight());
holder.height.setText("Height:" + item.getHeight());
holder.reach.setText("Reach: " + item.getReach());
holder.btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final int id = item.getId();
LayoutInflater layoutInflater = activity.getLayoutInflater();
View view1 = layoutInflater.inflate(R.layout.activity_edit,null);
final EditText input_name = (EditText) view1.findViewById(R.id.Name);
final EditText input_age = (EditText) view1.findViewById(R.id.Age);
final EditText input_weight = (EditText) view1.findViewById(R.id.Weight);
final EditText input_height = (EditText) view1.findViewById(R.id.Height);
final EditText input_reach = (EditText) view1.findViewById(R.id.Reach);
final Button btnSave = (Button) view1.findViewById(R.id.btnSave);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setView(view1).setTitle("Edit Records").setNegativeButton("close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
final Functions functions = new Functions(activity);
final Item _items = functions.getSingleItem(id);
input_name.setText(_items.getName());
input_age.setText(_items.getAge());
input_weight.setText(_items.getWeight());
input_height.setText(_items.getHeight());
input_reach.setText(_items.getReach());
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = input_name.getText().toString();
String age = input_age.getText().toString();
String weight = input_weight.getText().toString();
String height = input_height.getText().toString();
String reach = input_reach.getText().toString();
_items.setName(name);
_items.setAge(age);
_items.setWeight(weight);
_items.setHeight(height);
_items.setReach(reach);
functions.Update(_items);
Toast.makeText(activity, name + " updated.", Toast.LENGTH_SHORT).show();
((MainActivity)activity).fetchRecords();
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final int id = item.getId();
final String last = item.getName();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Delete").setMessage("Are you sure you want to delete " + last+"?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Functions functions = new Functions(activity);
functions.DeleteItem(id);
((MainActivity)activity).fetchRecords();
Toast.makeText(activity,last + " deleted.",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, age, weight, height, reach;
TextView btnEdit,btnDelete;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
age = (TextView) itemView.findViewById(R.id.age);
weight = (TextView) itemView.findViewById(R.id.weight);
height = (TextView) itemView.findViewById(R.id.height);
reach = (TextView) itemView.findViewById(R.id.reach);
btnDelete = (TextView) itemView.findViewById(R.id.btnDelete);
btnEdit = (TextView) itemView.findViewById(R.id.btnEdit);
}
}
}
答案 0 :(得分:0)
在Insert方法中删除此行:
contentValues.put(fighter.ID,item.getId());
ID会自动生成。