[英]Delete Item From Recycler View on Button Click.Button is Set from outside of Recycler View?
我想要一个RecyclerView
外部的删除按钮和一个将接受用户输入的Edit Text
,当我单击删除按钮时,应删除Recyclerview
中与Edittext
文本匹配的Edittext
。 我怎样才能做到这一点?
主活动.java :
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Adapter adapter;
ArrayList<ModelClass> arrayList;
RecyclerView.LayoutManager layoutManager;
EditText edit_name, edit_desc;
Spinner spinner;
Button btm_save, btn_delete;
String Name, Description, Spinner;
SQLiteDatabase sqLiteDatabase;
SqliteHelper sqliteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_name = findViewById(R.id.edit_name);
edit_desc = findViewById(R.id.edit_desc);
btm_save = findViewById(R.id.btn_save);
btn_delete = findViewById(R.id.btn_view);
spinner = findViewById(R.id.spinner);
sqliteHelper = new SqliteHelper(this);
sqLiteDatabase = sqliteHelper.getReadableDatabase();
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Options, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
recyclerView = findViewById(R.id.recylerView);
arrayList = new ArrayList<>();
adapter = new Adapter(this, arrayList);
recyclerView.setAdapter(adapter);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
getAllData();
edit_name.addTextChangedListener(textWatcher);
edit_desc.addTextChangedListener(textWatcher);
btm_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Name = edit_name.getText().toString();
Description = edit_desc.getText().toString();
Spinner = spinner.getSelectedItem().toString();
if (!ValidateUser(Name)) {
if (!TextUtils.isEmpty(Spinner)) {
ModelClass modelClass = new ModelClass(Name, Spinner, Description);
arrayList.add(modelClass);
adapter.notifyItemInserted(arrayList.size());
InsertData();
} else {
Toast.makeText(MainActivity.this, "Please Select Experience", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "User name Already Exits", Toast.LENGTH_SHORT).show();
}
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Integer deletedRows = sqliteHelper.deleteData(edit_name.getText().toString());
if (deletedRows > 0) {
Toast.makeText(MainActivity.this, "Record Deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Record Not Found", Toast.LENGTH_SHORT).show();
}
}
});
}
private void InsertData() {
boolean InsertSuccessfully = sqliteHelper.InsertData(Name, Description, Spinner);
if (InsertSuccessfully) {
Toast.makeText(this, "Record Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Record Not Inserted", Toast.LENGTH_SHORT).show();
}
}
public void getAllData() {
Cursor cursor = sqliteHelper.retriveData();
while (cursor.moveToNext()) {
ModelClass modelClass = new ModelClass(cursor.getString(cursor.getColumnIndex(SqliteHelper.COL_2)),
cursor.getString(cursor.getColumnIndex(SqliteHelper.COL_3)),
cursor.getString(cursor.getColumnIndex(SqliteHelper.COL_4)));
arrayList.add(modelClass);
}
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Name = edit_name.getText().toString();
Description = edit_desc.getText().toString();
btm_save.setEnabled(!Name.isEmpty() && !Description.isEmpty());
}
@Override
public void afterTextChanged(Editable editable) {
}
};
public boolean ValidateUser(String name) {
SQLiteDatabase database = sqliteHelper.getWritableDatabase();
Cursor cursor = database.query(SqliteHelper.TABLE_NAME, null, SqliteHelper.COL_2 + "=?", new String[]{name},
null, null, null);
int i = cursor.getCount();
if (i > 0) {
return true;
} else {
return false;
}
}
}
活动_main.xml :
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/Name"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinner"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.883"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="76dp"
android:layout_marginLeft="76dp"
android:layout_marginTop="28dp"
android:enabled="false"
android:text="Save Data"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_name" />
<EditText
android:id="@+id/edit_desc"
android:layout_width="match_parent"
android:layout_height="60dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylerView"
android:layout_width="match_parent"
android:layout_height="550dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_desc"
app:layout_constraintVertical_bias="0.036" />
<Button
android:id="@+id/btn_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="88dp"
android:layout_marginRight="88dp"
android:text="Delete Data"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn_save"
app:layout_constraintTop_toBottomOf="@+id/spinner" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果您的项目使用您使用的代码被删除,那么您只需要刷新您的列表:
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Integer deletedRows = sqliteHelper.deleteData(edit_name.getText().toString());
if (deletedRows > 0) {
Toast.makeText(MainActivity.this, "Record Deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Record Not Found", Toast.LENGTH_SHORT).show();
}
getAllData();
adapter.notifyDataSetChanged();
}
});
在 modelClass 对象的 ArrayList 中找到字符串,一旦找到,请查看该索引并从数组列表中删除该条目,并将其通知给 recyclerView 的适配器。
int count = 0;
for(ModelClass mod : arrayList) {
if(mod.name.equalsIgnoreCase(editText.getString)) {
arrayList.remove(mod);
adapter.notifyItemRemoved(count);
break;
}
count++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.