1

I have a function from where I display a layout dynamically. It shows an event created for a day. It has a start time and end time as a parameter. For testing purpose I had use array of a float with some values . But now I want to add the start time and end time values to this function which I am getting from another activity. I want them to use in an array dynamically and use it in a function.

Following is my function :

 private void createEvent(LayoutInflater inflater, ViewGroup dayplanView,float startTime, float endTime) {
    View eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();

    float margin = (startTime * 60);
    float distance = (endTime - startTime);
    float height = (distance * 60);

    int result = ((int)margin);
    int result1 = ((int)height);

    layoutParams.topMargin = dpToPixels(result+10);
    layoutParams.height = dpToPixels(result1);

    eventView.setLayoutParams(layoutParams);
    dayplanView.addView(eventView);
}

I am calling it onCreate view :

 ViewGroup dayplanView = (ViewGroup)view.findViewById(R.id.hoursRelativeLayout);


        float[] startTimes = new float[] {2.5f,8f,4f,11f,5f,8.5f,12.3f,15.3f,21f,1f};
        float[] endTimes = new float[] {3f,9f,7f,12.5f,7.5f,11f,1.5f,16f,22f,2f};


        for(int i = 0; i < s.size(); i++) {
            createEvent(inflater, dayplanView, startTimes[i], endTimes[i]);
        }

I am getting from minutes and to minutes onActivity result and this I ant to use in an array and in function :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    // check if the request code is same as what is passed  here it is 2
    if(requestCode==1) {

        fromMinutes = data.getIntExtra("fromMinute",0);
        toMinutes = data.getIntExtra("toMinute",0);


    }
}

Please help..

Edit :

ArrayList<Integer> s = new ArrayList<Integer>();
        s.add(fromMinutes);

        ArrayList<Integer> e = new ArrayList<Integer>();
        e.add(toMinutes);


        for(int i = 0; i < s.size(); i++) {
            createEvent(inflater, dayplanView,s[i], e[i]);
        }

I am getting error on s[i] as array type expected found java.util.array list.

private void createEvent(LayoutInflater inflater, ViewGroup dayplanView,int fromMinutes, int toMinutes) {
        View eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();

        float margin = (fromMinutes * 60);
        float distance = (fromMinutes - toMinutes);
        float height = (distance * 60);

        int result = ((int)margin);
        int result1 = ((int)height);

        layoutParams.topMargin = dpToPixels(result+10);
        layoutParams.height = dpToPixels(result1);

        eventView.setLayoutParams(layoutParams);
        dayplanView.addView(eventView);
    }

Is this right??

enter image description here

5
  • Instead of array use arraylist. You can also convert your existing array to arraylist and then add new element to arraylist Commented Jan 25, 2016 at 6:11
  • Array is a static instance. You can't set new length to it. All what you can do, is create new array with new length. Or, for example, use List. Commented Jan 25, 2016 at 6:15
  • Thank you for reply.. I tried using array list like this : ArrayList<Integer> s = new ArrayList<Integer>(); s.add(fromMinutes); ArrayList<Integer> e = new ArrayList<Integer>(); e.add(toMinutes); ----------------But I am not sure how can i use elements from array as a parameter to a function? Like I am using start Times and End Times in above function. And that i want to use for further calculation also. Can you show me in a code please. @Vivek Mishra Commented Jan 25, 2016 at 6:15
  • I know the option of a array list but i am unable to code it. Can you please show me in a code? @Majestic Commented Jan 25, 2016 at 6:18
  • If you want to use the array list in more than one method, try declaring the array list as class variable. Commented Jan 25, 2016 at 6:21

2 Answers 2

1

You should use ArrayList instead of Array, for dynamically add, remove, replace operations.

Sign up to request clarification or add additional context in comments.

2 Comments

how can i use as a parameter to a function?@Ashutosh Verma
@user5669913 it may help you to check my answer how to pass parameter
0

Instead of array use arraylist. You can also convert your existing array to arraylist and then add new element to arraylist.

You can add you array data to arraylist in one go as you already have arrays created like this

List<Integer> temp = Arrays.asList(speciesArr);

To get data from arraylist it is same that of an array with slight modification.

ArrayList<Integer> s = new ArrayList<Integer>(); 
s.add(fromMinutes); 

And to get data simply use

s.get(i);

7 Comments

Can I use s.get(i) as parameter to a function? I am not sure about this.
yes you can do it as you are also doing it with the array. As your parameters are in float you have to typecast it to float or either create arraylist of float type
No I want Array List of integer type. I am still confused can you show me how to use from minutes as a parameter to a function please.@ Vivek Misshra
Have you added your data to arraylist??
yes i have added like this : ArrayList<Integer> s = new ArrayList<Integer>(); s.add(fromMinutes); ArrayList<Integer> e = new ArrayList<Integer>(); e.add(toMinutes);
|

Your Answer

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