user2872856
user2872856

Reputation: 2051

Go to position of ArrayList according to month

I have an ArrayList which contains HTML files according to month. What I am trying to accomplish is, let say if current month is Jan, then the first of the list is jan.html, followed by feb.html, mar.html, etc. If current month is Feb, the position of ArrayList will directly move to the second one, which is feb.html, followed by mar.html and preceded by jan.html.

mListViews = new ArrayList<View>();
        addView(mListViews, "file:///android_asset/jan.html");
        addView(mListViews, "file:///android_asset/feb.html");
        addView(mListViews, "file:///android_asset/mar.html");
        addView(mListViews, "file:///android_asset/apr.html");
        addView(mListViews, "file:///android_asset/may.html");
        addView(mListViews, "file:///android_asset/jun.html");
        addView(mListViews, "file:///android_asset/jul.html");
        addView(mListViews, "file:///android_asset/aug.html");
        addView(mListViews, "file:///android_asset/sep.html");
        addView(mListViews, "file:///android_asset/oct.html");
        addView(mListViews, "file:///android_asset/nov.html");
        addView(mListViews, "file:///android_asset/dec.html");

This is my addView:

private void addView(List<View> viewList,String url)
    {
        WebView webView=new WebView(this);
        webView.loadUrl(url);
        viewList.add(webView);
    }

I know I need to use

Calendar c = Calendar.getInstance();
        int month = c.get(Calendar.MONTH);
if (month == 0) {
do something
}
else if (month == 1) {
do something
}

However, I have no idea on the "do something" part. Is it possible for me to do this?

Upvotes: 0

Views: 52

Answers (1)

Yazan
Yazan

Reputation: 6082

you can directly get the item from Arraylist (if list is populated) using get(index) no need for if statement

Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
mListViews.get(month);

EDIT: by default ArrayList will add items in order with index =0, then 1, ...etc

unless you really need to set the index when adding, you don't need to do it

you will have to modify the function addView() as following:

private void addView(int index, List<View> viewList,String url)
    {
        WebView webView=new WebView(this);
        webView.loadUrl(url);
        viewList.add(index,webView);
    }

and when calling addView(), just like this

addView(0, mListViews, "file:///android_asset/jan.html");
addView(1, mListViews, "file:///android_asset/feb.html");

but again, passing, 0, 1, 2 ... is the default for arrayList it will change nothing unless you want to change the order of month-name with index

EDIT2:

i think you need to use Collections.rotate() revert the old code, and after adding all items to list rotate the list to current month, a lucky guess would be

Collections.rotate(mListViews , mListViews.size()-month);

you need to read more about it and do some trials.

Upvotes: 1

Related Questions