2016-07-04 12 views
3

メソッドonClick()でRecyclerViewのオブジェクトの現在の位置を取得しようとしているときにこのエラーが発生します。ここにコードの断片があります:java.lang.IndexOutOfBoundsException:無効なインデックス1、セクションRecyclerViewでサイズが1である

@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) 
+2

JavaのArraylistsはゼロベースです。 'Notification notification = mNotifications.get(Position-1);試してみてください。 – Jens

+0

@Jensはこのコメントを回答として投稿しています。これは私の問題を解決したため、おそらく他の人にも役立ちます。 –

+0

しました。アップしてください、そして/または答えを受け入れてください。 – Jens

答えて

2

のArrayListをゼロベースにしています。試すNotification notification = mNotifications.get(Position-1);

0

はこれを試してください:Javaで

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(); 
    } 
}; 
関連する問題