当我试图在方法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();
}
整个错误日志:
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)
答案 0 :(得分:2)
Java中的Arraylists是基于零的。试试Notification notification = mNotifications.get(Position-1);
答案 1 :(得分:0)
试试这个:
m_OperatorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position > 1){
// send your request
}
}
...
});