java.lang.IndexOutOfBoundsException:索引1无效,在分段回收视图中大小为1
在方法java.lang.IndexOutOfBoundsException:索引1无效,在分段回收视图中大小为1,java,android,Java,Android,在方法onClick()中尝试获取RecyclerView中对象的当前位置时,出现此错误。下面是一段代码: @Override public void onClick(View v) { if (v == btnAccept) { int position = (Integer) v.getTag(); Notification notification = mNotifications.get(p
onClick()
中尝试获取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);
}
所以我试图在列表中找到当前选定对象的位置。此列表还有一些分段项。
在这里,我将数据设置为对象数组:
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();
}
试试这个:
notificationHolder.checkParentView.setTag(itemsData.ge(position));
notificationHOlder.checkParentView.setOnClickListener(checkedListener);
private View.OnClickListener checkedListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int positon = (Integer) v.getTag();
}
};
Java中的ArrayList是基于零的。Try
Notification Notification=mNotifications.get(位置1)代码>Java中的ArrayList是基于零的。TryNotification Notification=mNotifications.get(位置1)
@Jens将此评论作为答案发布,因为它解决了我的问题,可能也会帮助其他人。我已经这样做了。请投票和/或接受答案。
notificationHolder.checkParentView.setTag(itemsData.ge(position));
notificationHOlder.checkParentView.setOnClickListener(checkedListener);
private View.OnClickListener checkedListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int positon = (Integer) v.getTag();
}
};