Ich bekomme diesen Fehler, wenn ich versuche, die aktuelle Position des Objekts in RecyclerView in Methode onClick()
zu erhalten. Hier ist ein Fragment des Codes:java.lang.IndexOutOfBoundsException: Invalid Index 1, Größe ist 1 in Sectored RecyclerView
@Override
public void onClick(View v) {
if (v == btnAccept) {
int position = (Integer) v.getTag();
Notification notification = mNotifications.get(position); // Here is an error
mFragmentPageOne.acceptMembership(notification.getGroupId());
} else if (v == btnDecline) {
}
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, Notification item, @ViewType int viewType) {
NotificationHolder notificationHolder = (NotificationHolder) holder;
final int position = holder.getAdapterPosition();
notificationHolder.tvNotification.setText(
item.getNotification() + " ti je poslao zahtev za clanstvo u ekipu "
+ item.getGroupName());
long timestamp = DateUtil.getDifference(item.getTimestamp());
// Converting timestamp into x ago format
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(String.valueOf(timestamp)),
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
notificationHolder.tvTimestamp.setText(timeAgo);
notificationHolder.btnAccept.setTag(position);
notificationHolder.btnDecline.setTag(position);
setAnimation(notificationHolder.mRelativeLayout, position);
}
So versuche ich eine Position des aktuellen ausgewählten Objekts in der Liste zu finden. Diese Liste enthält auch einige Abschnitte. ich hier Daten auf das Array von Objekten einstellen:
try {
JSONObject obj = new JSONObject(response);
boolean error = obj.getBoolean("error");
JSONArray ary = obj.getJSONArray("membership");
if (!error) {
for (int i = 0; i < ary.length(); i++) {
JSONObject innerObj = ary.getJSONObject(i);
Notification notification = new Notification();
notification.setNotification(innerObj.getString("name"));
notification.setTimestamp(innerObj.getString("createdAt"));
notification.setGroupId(innerObj.getInt("group_id"));
notification.setGroupName(innerObj.getString("group_name"));
notification.setCategory("Membership Requests");
mNotificationList.add(notification);
}
mAdapter = new NotificationHeaderListAdapter(getActivity(), mNotificationList, FragmentPageOne.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration(getActivity()));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
Das ganze Fehlerprotokoll:
Process: com.solaris.timster, PID: 4850
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.solaris.timster.adapter.NotificationHeaderListAdapter$NotificationHolder.onClick(NotificationHeaderListAdapter.java:99)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10814)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Arraylists in Java sind Zero basiert. Probiere 'Notification notification = mNotifications.get (Position-1);' – Jens
@Jens poste diesen Kommentar als Antwort, weil er mein Problem gelöst hat und wahrscheinlich auch anderen helfen wird. –
habe ich getan. Bitte upvote und/oder akzeptiere die Antwort. – Jens