У меня есть RecyclerView
с одним видом текста внутри него, и у меня есть одна EditText
и две кнопки ( Кнопка добавления & Кнопка удаления ), которые RecyclerView
беретвсе значения из EditText
после того, как я нажимаю кнопку Добавить , и я удаляю последний элемент в RecyclerView
с помощью Удалить кнопку , но когда список пуст, и я нажимаю Кнопка удаления приложение вылетает.
Я бы хотел отключить Удалить кнопку , когда список пуст.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.brecyclerview.MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edit_ten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Score"
android:inputType="numberSigned|number" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_add"
android:text="Add"
android:enabled="false"
tools:ignore="OnClick" />
<Button
android:id="@+id/btn_undo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_undo"
android:text="Undo"
tools:ignore="OnClick" />
<Button
android:id="@+id/btn_new"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_new"
android:text="New Game" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:itemCount="8" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/singleRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/userImg"
android:src="@mipmap/ic_launcher"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="@+id/pNametxt"
android:text="User Name"
android:textSize="20sp"
android:layout_marginTop="6dp"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/userImg"
android:layout_toEndOf="@+id/userImg"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" />
</RelativeLayout>
<View
android:layout_below="@+id/singleRow"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f2f2f2" />
</RelativeLayout>
MainActivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
int total = 0;
Button button;
Button button1;
Button button2;
EditText editText;
TextView personName;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager layoutManager;
List<PersonUtils> personUtilsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_add);
button1 = (Button) findViewById(R.id.btn_undo);
button2 = (Button) findViewById(R.id.btn_new);
editText = (EditText) findViewById(R.id.edit_ten);
personName = (TextView) findViewById(R.id.pNametxt);
recyclerView = (RecyclerView) findViewById(R.id.recycleViewContainer);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
personUtilsList = new ArrayList<>();
mAdapter = new CustomRecyclerAdapter(this, personUtilsList);
editText.addTextChangedListener(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) {
String addvalue = editText.getText().toString().trim();
button.setEnabled(!addvalue.isEmpty());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == button2) {
recreate();
}}});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < personUtilsList.size(); i++) {
total = personUtilsList.get(i).getPersonName();
}
total += Integer.parseInt(editText.getText().toString());
personUtilsList.add(new PersonUtils(total));
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
editText.setText("");
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = mAdapter.getItemCount() -1;
removeItem(position);
}
});
}
public void removeItem(int position) {
if(position <= 0)
{ recreate();
}
else if(position == mAdapter.getItemCount() - 1)
{personUtilsList.remove(position);
mAdapter.notifyItemRemoved(position);
}
else
{
}
}
});}}
CustomRecyclerAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> {
private Context context;
private static List<PersonUtils> personUtils;
public CustomRecyclerAdapter(Context context, List personUtils) {
this.context = context;
this.personUtils = personUtils;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setTag(personUtils.get(position));
PersonUtils pu = personUtils.get(position);
holder.pName.setText(String.valueOf(pu.getPersonName()));
}
@Override
public int getItemCount() {
return (personUtils.size()>8)?8:personUtils.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView pName;
final Button deleteButton;
public ViewHolder(final View itemView) {
super(itemView);
pName = (TextView) itemView.findViewById(R.id.pNametxt);
deleteButton = (Button) itemView.findViewById(R.id.btn_undo);
}}}
PersonUtils.java
public class PersonUtils {
private Integer personName;
public Integer getPersonName() {
return personName;
}
public void setPersonName(Integer personName) {
this.personName = personName;
}
public PersonUtils(Integer personName) {
this.personName = personName;
}
}