gpt4 book ai didi

android - 在 RecyclerView 中删除最后一项时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-05 00:16:29 34 4
gpt4 key购买 nike

我正在使用 RecyclerView 并从数据库填充数据并将其存储到 arrayLists 中。我还在回收器 View 中添加了一个 onLongClickListener() ,它删除了长按的行。一切正常,但当我删除最后一个应用程序时,应用程序崩溃,并且出现 Java.Lang.IndexOutOfBoundsException 索引:0 大小:0。

这是我的代码

public class MainActivity extends AppCompatActivity implements AlarmRecyclerViewInterface {

FloatingActionButton mAlarmAddButton;
RecyclerView mRecyclerView;
SQLiteDatabase db;
AlarmsDBhelperClass mAlarmsDBhelperClass;
ArrayList<String> nameArrayList = new ArrayList<>();
ArrayList <String> modeArrayList = new ArrayList<>();
ArrayList<String> repeatArrayList = new ArrayList<>();
ArrayList<String> hoursArrayList = new ArrayList<>();
ArrayList<String> minArrayList = new ArrayList<>();
Switch mSwitch;
AlarmAdapter alarmAdapter = new AlarmAdapter(hoursArrayList,minArrayList,modeArrayList,repeatArrayList,nameArrayList,this);
ImageView emptyImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//Initializing RecyclerView, DatabaseHelperClass, FAB button, The ON OFF switch & the empty ImageView
mAlarmsDBhelperClass = new AlarmsDBhelperClass(getApplicationContext());
mAlarmAddButton = findViewById(R.id.btnAlarmADD);
mRecyclerView = findViewById(R.id.alarmList);
mSwitch = findViewById(R.id.switchONOFF);
emptyImageView = findViewById(R.id.empty_view);

//DividerItemDecoration class is used for getting a vertical line between rows of RecyclerView
DividerItemDecoration itemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);

//Getting a writable reference of the Database.
db = mAlarmsDBhelperClass.getWritableDatabase();

//Retrieving values from the database and storing them in custom ArrayLists
boolean isDataEmpty = getAlarm(db);

//Checking if our arrayList is empty? if yes then display some empty list text or an image
if (!isDataEmpty){
mRecyclerView.setVisibility(View.GONE);
emptyImageView.setImageResource(R.drawable.no_alarm_black_white);
emptyImageView.setVisibility(View.VISIBLE);
}
else {
mRecyclerView.setVisibility(View.VISIBLE);
emptyImageView.setVisibility(View.GONE);
}

//FAB Event handling
mAlarmAddButton.setImageResource(R.drawable.addalarm);
mAlarmAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mIntent = new Intent(getApplicationContext(), AddAlarm.class);
startActivity(mIntent);
}
});

//Warping up with the recyclerView
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mRecyclerView.setAdapter(alarmAdapter);
mRecyclerView.setHasFixedSize(true);
}

public boolean getAlarm(SQLiteDatabase db) {
Cursor cursor = db.rawQuery("SELECT * FROM alarms", new String[]{});
boolean rowExists;
if (cursor.moveToFirst()) {
do {
nameArrayList.add(cursor.getString(2));
modeArrayList.add(cursor.getString(3));
repeatArrayList.add(cursor.getString(4));
hoursArrayList.add(Integer.toString(cursor.getInt(5)));
minArrayList.add(Integer.toString(cursor.getInt(6)));
rowExists = true;
} while (cursor.moveToNext());
}else {
rowExists = false;
}
cursor.close();
return rowExists;
}

//RecyclerView's onClick()
@Override
public void onItemClick(int position) {
Toast.makeText(this, "Alarm Clicked !", Toast.LENGTH_SHORT).show();
}

//RecyclerView's onLongClick()
@Override
public void onLongItemClick(int position) {
hoursArrayList.remove(position);
//Updating the recyclerView
alarmAdapter.notifyItemRemoved(position);
//Deleting the row from the database
db.delete("alarms","hours=?",new String[]{hoursArrayList.get(position)});
Toast.makeText(this,"Alarm Deleted !", Toast.LENGTH_SHORT).show();
}
}

这是我的 RecyclerView 适配器

public class AlarmAdapter extends RecyclerView.Adapter<AlarmAdapter.AlarmView> {
//Variables for the main recycler view
private ArrayList<String> hoursArrayList;
private ArrayList<String> minArrayList;
private ArrayList<String> modeArrayList;
private ArrayList<String> repeatArrayList;
private ArrayList<String> nameArrayList;
private AlarmRecyclerViewInterface mInterface;

public AlarmAdapter(ArrayList<String> hours,ArrayList<String> mins,ArrayList<String> mode,ArrayList<String> repeat,ArrayList<String> name,AlarmRecyclerViewInterface mInterface){
this.hoursArrayList = hours;
this.minArrayList = mins;
this.modeArrayList = mode;
this.nameArrayList = name;
this.repeatArrayList = repeat;
this.mInterface = mInterface;
}

@NonNull
@Override
public AlarmView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.alarm_profile,parent,false);
return new AlarmView(view);
}

@Override
public void onBindViewHolder(@NonNull AlarmView holder, int position) {
if(Integer.parseInt(hoursArrayList.get(position)) < 10 ){
holder.hours.setText("0"+hoursArrayList.get(position));
}else {
holder.hours.setText(hoursArrayList.get(position));
}
if (Integer.parseInt(minArrayList.get(position)) < 10){
holder.mins.setText("0"+minArrayList.get(position));
}else {
holder.mins.setText(minArrayList.get(position));
}
holder.repeat.setText(repeatArrayList.get(position));
holder.mode.setText(modeArrayList.get(position));
holder.name.setText(nameArrayList.get(position));
}

@Override
public int getItemCount() {
return hoursArrayList.size();
}

/*ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
nameArrayList.remove(viewHolder.getAdapterPosition());
modeArrayList.remove(viewHolder.getAdapterPosition());
repeatArrayList.remove(viewHolder.getAdapterPosition());
hoursArrayList.remove(viewHolder.getAdapterPosition());
minArrayList.remove(viewHolder.getAdapterPosition());
}
};*/

public class AlarmView extends RecyclerView.ViewHolder{
TextView hours,mins,repeat,name,mode;
public AlarmView(@NonNull View itemView) {
super(itemView);
hours = itemView.findViewById(R.id.txtHOUR);
mins = itemView.findViewById(R.id.txtMins);
repeat = itemView.findViewById(R.id.txtRepeatDays);
name = itemView.findViewById(R.id.txtName);
mode = itemView.findViewById(R.id.txtMode);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mInterface.onItemClick(getAdapterPosition());
}
});

itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mInterface.onLongItemClick(getAdapterPosition());
return true;
}
});

}
}

}

感谢您的帮助!

最佳答案

  • 崩溃:- IndexOutOfBoundsException

    • 原因:- 您从列表中删除了数据,之后您再次从列表中提取该值以从数据库中删除数据,此时数据库会崩溃,因为删除数据时它没有任何值。

    • 解决方案:- 您可以先从数据库中删除,然后再从列表中删除。

  • Example :-

    Replace below onLongClick() Override method in your class.
//RecyclerView's onLongClick()
@Override
public void onLongItemClick(int position) {

// Best way to remove data from list
if(hoursArrayList != null && hoursArrayList.size() > 0){

//Deleting the row from the database
db.delete("alarms","hours=?",new String[]{hoursArrayList.get(position)});

//Delete from your list if you successfully removed data from your database.
hoursArrayList.remove(position);

//Updating the recyclerView
if(alarmAdapter){
alarmAdapter.notifyItemRemoved(position);
}

//Message to show User.
Toast.makeText(this,"Alarm Deleted !", Toast.LENGTH_SHORT).show();
}

}

关于android - 在 RecyclerView 中删除最后一项时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59915227/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com