Java 单击按钮从回收器视图中删除项目。是否从回收器视图外部设置该按钮?
我希望在Java 单击按钮从回收器视图中删除项目。是否从回收器视图外部设置该按钮?,java,android,android-studio,android-layout,Java,Android,Android Studio,Android Layout,我希望在RecyclerView之外有一个删除按钮和一个编辑文本,它将接受用户输入,当我点击删除按钮时,RecyclerView中与编辑文本匹配的行应该被删除。我怎样才能做到这一点 MainActivity.java: public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; Adapter adapter; ArrayList<ModelCl
RecyclerView
之外有一个删除按钮和一个编辑文本
,它将接受用户输入,当我点击删除按钮时,RecyclerView
中与编辑文本
匹配的行应该被删除。我怎样才能做到这一点
MainActivity.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;
}
}
}
public类MainActivity扩展了AppCompatActivity{
回收视图回收视图;
适配器;
ArrayList ArrayList;
RecyclerView.LayoutManager LayoutManager;
编辑文本编辑名称,编辑描述;
纺纱机;
按钮btm_保存,btn_删除;
字符串名称、描述、微调器;
SQLiteDatabase SQLiteDatabase;
SqliteHelper SqliteHelper;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_name=findviewbyd(R.id.edit_name);
edit_desc=findviewbyd(R.id.edit_desc);
btm_save=findviewbyd(R.id.btn_save);
btn_delete=findviewbyd(R.id.btn_视图);
微调器=findViewById(R.id.spinner);
sqliteHelper=新的sqliteHelper(this);
sqLiteDatabase=sqliteHelper.getReadableDatabase();
ArrayAdapter ArrayAdapter=ArrayAdapter.createFromResource(这个,R.array.Options,android.R.layout.simple\u微调器\u项);
arrayAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spinner.setAdapter(阵列适配器);
recyclerView=findViewById(R.id.recylerView);
arrayList=新的arrayList();
适配器=新适配器(此,arrayList);
recyclerView.setAdapter(适配器);
layoutManager=新的LinearLayoutManager(此);
recyclerView.setLayoutManager(layoutManager);
getAllData();
编辑_name.addTextChangedListener(textWatcher);
编辑_desc.addTextChangedListener(textWatcher);
btm_save.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Name=edit_Name.getText().toString();
Description=edit_desc.getText().toString();
Spinner=Spinner.getSelectedItem().toString();
如果(!ValidateUser(名称)){
如果(!TextUtils.isEmpty(微调器)){
ModelClass ModelClass=新的ModelClass(名称、微调器、说明);
add(modelClass);
adapter.notifyItemInserted(arrayList.size());
InsertData();
}否则{
Toast.makeText(MainActivity.this,“请选择体验”,Toast.LENGTH_SHORT.show();
}
}否则{
Toast.makeText(MainActivity.this,“用户名已经存在”,Toast.LENGTH_SHORT.show();
}
}
});
btn_delete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
整数deletedRows=sqliteHelper.deleteData(编辑_name.getText().toString());
如果(deletedRows>0){
Toast.makeText(MainActivity.this,“记录已删除”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(MainActivity.this,“未找到记录”,Toast.LENGTH_SHORT.show();
}
}
});
}
私有void InsertData(){
boolean InsertSuccessfully=sqliteHelper.InsertData(名称、描述、微调器);
如果(插入成功){
Toast.makeText(这是“插入的记录”,Toast.LENGTH_SHORT).show();
}否则{
Toast.makeText(这是“未插入记录”,Toast.LENGTH_SHORT).show();
}
}
public void getAllData(){
Cursor Cursor=sqliteHelper.retriveData();
while(cursor.moveToNext()){
ModelClass ModelClass=新的ModelClass(cursor.getString(cursor.getColumnIndex(SqliteHelper.COL_2)),
cursor.getString(cursor.getColumnIndex(SqliteHelper.COL_3)),
getString(cursor.getColumnIndex(SqliteHelper.COL_4));
add(modelClass);
}
}
私有TextWatcher TextWatcher=新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i1,int i2){
}
@凌驾
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());
}
@凌驾
public void PostTextChanged(可编辑){
}
};
公共布尔ValidateUser(字符串名称){
SQLiteDatabase=sqliteHelper.getWritableDatabase();
Cursor Cursor=database.query(SqliteHelper.TABLE_NAME,null,SqliteHelper.COL_2+“=?”,新字符串[]{NAME},
空,空,空);
int i=cursor.getCount();
如果(i>0){
返回true;
}否则{
返回false;
}
}
}
activity_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中查找字符串,找到后,查看该索引并从
int count = 0;
for(ModelClass mod : arrayList) {
if(mod.name.equalsIgnoreCase(editText.getString)) {
arrayList.remove(mod);
adapter.notifyItemRemoved(count);
break;
}
count++;
}