1

I am creating calenderview. and want to add multiple days events like google calender app so in this I am adding event at each day.I want to add one textview to linearlayout at position 2.at position 0 and 1 there is no any views are added, this is my code to insert view.

`

TextView tv = new TextView(context);
tv.setBackgroundResource(R.drawable.event_view);
int height 
=context.getResources().getDimensionPixelSize(R.dimen.event_image_size);
LinearLayout.LayoutParams params = new 
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height); 
int previousViewPosition = AppConstants.eventList.get(i-1).getEvent_count();                                        
AppConstants.eventList.get(i).setEvent_count(previousViewPosition);
tv.setPadding(0, 0, 0, 0);
tv.setText(AppConstants.eventList.get(i).getStrName());
params.setMargins(0, 0, 0, 1);
tv.setLayoutParams(params);
tv.setTextSize(8);
tv.setTextColor(context.getResources().getColor(R.color.white));
ll_event_image.addView(tv, previousViewPosition);

`

it gives me error IndexOutOfBound.

05-02 11:57:17.162 8922-8922/com.calender.demo.calendar_api W/System.err: java.lang.IndexOutOfBoundsException: index=2 count=1 05-02 11:57:17.187 8922-8922/com.calender.demo.calendar_api W/System.err: at android.view.ViewGroup.addInArray(ViewGroup.java:4542) at android.view.ViewGroup.addViewInner(ViewGroup.java:4463) at android.view.ViewGroup.addView(ViewGroup.java:4218) 05-02 11:57:17.188 8922-8922/com.calender.demo.calendar_api W/System.err: at com.desai.vatsal.mydynamiccalendar.DateListAdapter$DateViewHolder.setDates(DateListAdapter.java:278) at com.desai.vatsal.mydynamiccalendar.DateListAdapter.onBindViewHolder(DateListAdapter.java:575) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714) at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)

5
  • 1
    You can't add a view to an index 2 which is not there as your LinearLayout doesn't have any view at index 0 & index 1. Commented May 2, 2018 at 6:51
  • So it is pretty obvious that you will get IndexOutOfBoundsException. Commented May 2, 2018 at 6:53
  • Apart from this your LinearLayout works in linear fashion. To add a View to index 1, you got to have a view at index 0. Commented May 2, 2018 at 6:57
  • I know that but i want solution to add views like this......and I got my answer...thank you. Commented May 2, 2018 at 7:25
  • That's great.!!!!! Commented May 2, 2018 at 7:28

3 Answers 3

4

Change the addView line

if(previousViewPosition >= ll_event_image.getChildCount())
   ll_event_image.addView(tv);
else
   ll_event_image.addView(tv, previousViewPosition);
Sign up to request clarification or add additional context in comments.

1 Comment

actually i am displaying multiple day event to calender app...So this code doesn't work.It doesn't display line in all days at same position
1

An IndexOutOfBoundsException is usually caused when you are trying to access a position that doesn't exists in your arrayfor instance.

In your case, you are trying to access am object at position index = 2 but your array has only 2 elements position = 0 and position = 1

EDIT: The problem is mostly cause by this line

AppConstants.eventList.get(i-1).getEvent_count(); <- Make sure you are not getting out of eventList bounds.

EDIT 2:

After reading more carefully, you are also trying to add a view in your LinearLayout at previousPosition which, in this case might not be a valid position.

3 Comments

but i m trying to add view to linearlayout ....there is no limit to add view to layout ..why i m getting this error?
problem is caused at this point.....ll_event_image.addView(tv, previousViewPosition);
Yes, my bad, when you are adding views in a LinearLayout you don't really have to specify the position, unless you already have some views and you want to add the new view before existing ones
1

From what I understand, you are trying to add a view at index 2 in LinearLayout but the LinearLayout does not have any view at indexes 0 and 1. You need to first add a view at index 0 and 1, only then you can access index 2. You can try adding an empty view if you want to leave that space blank but that's up to you and what you are trying to accomplish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.