在我的应用程序中,我在回收商视图中显示项目列表。我为回收商视图中的每个项目创建了两个功能编辑和删除。使用此应用程序,用户可以创建任务列表,可以编辑项目,也可以从回收器视图中删除项目。现在,如果我单击“编辑”,它就可以完美地工作。但是,如果我单击删除选项,则应用程序崩溃,并且消息数组索引在adaper类中因rance异常而崩溃。
这是我的适配器代码。我还提到了 代码中的行 ,其中我遇到了应用程序崩溃。
编辑适配器类
public class TodoAdapter extends RealmRecyclerViewAdapter<TodoModel, TodoAdapter.TaskHolder> {
public final static String INTENT_KEY_ID = "taskId";
public final static String INTENT_KEY_POSITION = "position";
public final static String DATE_FORMAT = "dd/MMM/yy";
private Realm realm;
public interface TaskListener {
}
private final TaskListener taskListener;
private final Context context;
public TodoAdapter(TaskListener taskListener, RealmResults<TodoModel> realmResults, Context context, Realm realm) {
super(realmResults, true);
this.taskListener = taskListener;
this.context = context;
this.realm = realm;
}
@Override
public TaskHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new TaskHolder( LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_row, parent, false));
}
@Override
public void onBindViewHolder(TaskHolder holder, final int position) {
final TodoModel task = getData().get(position);
holder.taskTextView.setText(task.getName());
holder.doneCheckBox.setChecked(task.isDone());
holder.timeTextView.setText( task.getTime() );
final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
final Date date = (task.getDate());
if (sdf.format(date).equals(sdf.format(getDate(0))))
holder.dateTextView.setText("Today");
else if (sdf.format(date).equals(sdf.format(getDate(-1))))
holder.dateTextView.setText("Yesterday");
else if (sdf.format(date).equals(sdf.format(getDate(1))))
holder.dateTextView.setText("Tomorrow");
else if (date.getTime() < getDate(6).getTime()) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
holder.dateTextView.setText( DateFormat.format("EEEE", calendar.getTime()).toString());
} else
holder.dateTextView.setText(sdf.format(task.getDate()));
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, TodoAddActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(INTENT_KEY_ID, getTask(position).getId());
context.startActivity(intent);
}
});
holder.doneCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TodoModel task = new TodoModel();
task.setId(getTask(position).getId());
task.setDate(getTask(position).getDate());
task.setName(getTask(position).getName());
task.setDone(((CheckBox) v).isChecked());
updateTask(position, task);
}
});
holder.deleteImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getTask(position).isDone()) {
removeTask(position);
notifyDataSetChanged();
return;
}
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Confirm Delete?");
alertDialog.setMessage("Do you want to delete the task you created?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
removeTask(position);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
}
public class TaskHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView taskTextView;
public TextView dateTextView;
public TextView timeTextView;
public ImageView deleteImageView;
public CheckBox doneCheckBox;
public TaskHolder(View itemView) {
super(itemView);
taskTextView = (TextView) itemView.findViewById( R.id.row_task_list_tv_name);
dateTextView = (TextView) itemView.findViewById(R.id.row_task_list_tv_date);
timeTextView=(TextView)itemView.findViewById( R.id.row_task_list_tv_time );
deleteImageView = (ImageView) itemView.findViewById(R.id.row_task_list_iv_delete);
doneCheckBox = (CheckBox) itemView.findViewById(R.id.row_task_list_cb_done);
cardView = (CardView) itemView.findViewById(R.id.cardView);
}
}
private Date getDate(int day) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, day);
return calendar.getTime();
}
protected TodoModel getTask(int position) {
return getData().get(position);
}
protected void updateTask(int position, TodoModel task) {
realm.beginTransaction();
TodoModel newTask = realm.where(TodoModel.class).equalTo("id", task.getId()).findFirst();
newTask.setDate(task.getDate());
newTask.setDone(task.isDone());
newTask.setName(task.getName());
realm.commitTransaction();
}
protected void removeTask(int position) {
TodoModel newTask = realm.where(TodoModel.class).equalTo("id", getTask(position).getId()).findFirst();
realm.beginTransaction();
newTask.deleteFromRealm();
realm.commitTransaction();
notifyItemRemoved(position);
}
}
事故报告
09-10 12:25:57.876 7332-7332/realmtest.com.to_do_list_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: realmtest.com.to_do_list_test, PID: 7332
java.lang.ArrayIndexOutOfBoundsException: Out of range in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_Collection.cpp line 133(requested: 1 valid: 1)
at io.realm.internal.Collection.nativeGetRow(Native Method)
at io.realm.internal.Collection.getUncheckedRow(Collection.java:386)
at io.realm.OrderedRealmCollectionImpl.get(OrderedRealmCollectionImpl.java:106)
at io.realm.RealmResults.get(RealmResults.java:53)
at io.realm.OrderedRealmCollectionImpl.get(OrderedRealmCollectionImpl.java:19)
at realmtest.com.to_do_list_test.activity.TodoAdapter.getTask(TodoAdapter.java:157)
at realmtest.com.to_do_list_test.activity.TodoAdapter$3.onClick(TodoAdapter.java:106)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
getData()的实现
@Nullable
public OrderedRealmCollection<T> getData() {
return adapterData;
}
TaskActivity类
public class TaskActivity extends AppCompatActivity implements TodoAdapter.TaskListener{
private static final int ADD_TASK_REQUEST_CODE = 1000;
private static final int EDIT_TASK_REQUEST_CODE = 1001;
private Realm realm;
private RecyclerView recyclerView;
private TodoAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_todo_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("ToDo List");
setSupportActionBar(toolbar);
realm = Realm.getDefaultInstance();
recyclerView =(RecyclerView)findViewById(R.id.activity_tasks_ll_task);
setUpRecycler();
// Variables
// Views
FloatingActionButton addFloatingActionButton = (FloatingActionButton) findViewById(R.id.activity_tasks_fab_add);
//Listeners
addFloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(getApplicationContext(), TodoAddActivity.class), ADD_TASK_REQUEST_CODE);
}
});
}
private void setAdapter() {
adapter = new TodoAdapter(this, realm.where(TodoModel.class).findAll(),this,realm);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void setUpRecycler() {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager since the cards are vertically scrollable
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
setAdapter();
}
}
图1
图2
通知数据设置更改()
可以使用
但若要显示动画以及更新列表中的项目位置
在序列中使用此代码
通知项目已移除(位置)
MH:用于删除项目和显示动画
notifyItemRangeChanged(position, ListSize);
删除条目后,您正在更改适配器的列表,但您不会通知适配器您已更改列表的大小。因此,您必须告诉适配器列表的大小已更改。
holder.deleteImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getTask(position).isDone()) { //Error for this line
removeTask(position);
notifyDataSetChanged(); <-------------------Add this line to your code.
return;
}
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Confirm Delete?");
alertDialog.setMessage("Do you want to delete the task you created?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
removeTask(position);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
每当您更改适配器列表的大小时,您都必须通过调用函数elle fyDataSetChanged()将更改显式地告诉适配器。
从 if (getTask(position).isDone()) {
中删除 return 语句,并将 AlertDialog
放在 if 的 else 子句中,如下所示。
holder.deleteImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getTask(position).isDone()) {
removeTask(position);
notifyDataSetChanged();
}else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Confirm Delete?");
alertDialog.setMessage("Do you want to delete the task you created?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
removeTask(position);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
}
});
我正在用AppWidgetHostViews填充RecyclerView。它们被排序一个比一个低,每个都有删除按钮旁边。这就是我如何设置RecyclerView和适配器(Event.Object是AppWidgetHostView): 这是保存AppWidgetHostViews的适配器: 因此它再次为适配器设置数据并通知数据集已更改。 但问题是,当我单击“删除”按钮时,项目没有被正确删除。Vie
这里是Android开发者新手。我在MainActivity中使用recyclerview,应用程序不断崩溃。 任何帮助都将受到赞赏! 编辑:对不起,我是新来的。我已经附加了Logcat。和其他xml文件。谢谢 这是我的代码: 列出你的布局。xml: activity_main.xml: } ProductAdapter。java类: } Logcat: 致命异常:主进程:e.wolverine2
删除项目后,“我的回收器”视图未更新。此recyclerView位于片段内部。我试过各种方法,但都不管用。 片段类中的适配器声明 回收服务适配器: } 调试项目时,我可以看到该项实际上正在从ArrayList中删除。但不在循环视图中更新。 删除后,如果滚动回收站视图,则从回收站视图中删除已删除的项。但不是没有滚动。
我将imageTextView编程为显示prenumeString的第一个字母,这是从编辑文本中获得的。问题是,每当编辑文本中出现所有字母时,应用程序就会崩溃。有什么想法吗? 代码:
在这里,我成功地添加了一个实时Firebase数据库图像滑块,它检索图像并在图像滑块中显示它们。一切正常。所有更新的图像和所有。除非我删除Firebase实时数据库中的子节点,否则应用程序会崩溃。 家庭碎片 XML代码 图像滑块适配器 图像滑块模型 这些都是源代码。注意:firebase根子节点中的所有值都在正确更新,根节点中的子节点除外。 这是崩溃日志报告 火基模型
Iam正在尝试编辑和删除RecyclerView中的项目。当我刷回收视图,我应该得到两个按钮。一个按钮用于编辑数据,另一个按钮用于从RecyclerView中删除数据。