How do you divide a list of strings into pages.

Discussion in 'Plugin Development' started by Mike111177, Jul 11, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Mike111177

    basically what i want to do is i have a list of strings and i want the player to index through them as list that consist of 5 of the strings. basically like using a command /players list 1 would list the first 5 /players list 2 would be the second the second 5 and so on...
     
  2. if you need this using the convertions api, you ccan use this class, else, build your own way based on this
    Code:java
    1. public class ChooseListPrompt extends MessagePrompt
    2. {
    3. private final String text;
    4. private final Prompt next;
    5.  
    6. public ChooseListPrompt(String prefix, Set<String> text, Prompt next)
    7. {
    8. this(prefix, text.iterator(), next);
    9.  
    10. }
    11.  
    12. protected ChooseListPrompt(String prefix, Iterator<String> text, Prompt next)
    13. {
    14.  
    15. StringBuilder b = new StringBuilder(prefix);
    16. for (int i = 0; i < 10; i++)
    17. {
    18. if (text.hasNext())
    19. {
    20. if (i != 0)
    21. {
    22. b.append(", ");
    23. }
    24. b.append(text.next());
    25. }
    26.  
    27. }
    28. this.text = b.toString();
    29. this.next = text.hasNext() ? new ChooseListPrompt(prefix, text, next) : next;
    30.  
    31. }
    32.  
    33. @Override
    34. protected Prompt getNextPrompt(ConversationContext paramConversationContext)
    35. {
    36. return this.next;
    37. }
    38.  
    39. @Override
    40. public String getPromptText(ConversationContext paramConversationContext)
    41. {
    42. return this.text;
    43. }
    44. }
     
  3. Offline

    Mike111177

    how would i use this class?
     
  4. Offline

    Tempelchat

    This may be something you could use:
    Code:
    import java.util.ArrayList;
    import java.util.List;
     
     
    public class Pager {
     
        static final int PAGELENGTH = 5;
     
        public Pager()
        {
            List<String> l = new ArrayList<String>();
         
            //generates a demo List with some Strings
            for(int i=0 ; i<18 ; i++)
            {
                l.add(new StringBuilder("string").append(i+1).toString());
            }
         
            //Fetches a page and displays it afterwards
            displayList(getPage(l, 4));
         
        }
     
        /**
        *
        * @param l A list containing all Strings,
        * @param pagenr The page number
        * @return List<String> containing all Strings on the page
        */
        public List<String> getPage(List<String> l, int pagenr)
        {
            List<String> page = new ArrayList<String>();
         
            int listart = (pagenr - 1) * PAGELENGTH;
            int liend  = listart + PAGELENGTH;
         
            for(int i=listart ; i<liend ;i++)
            {
                if(i < l.size())
                {
                    page.add(l.get(i));
                }
                else
                {
                    break;
                }
            }
         
            return page;
        }
     
        /**
        * Displays a List<String> for debugging.
        * @param l
        */
        public void displayList(List<String> l)
        {
            for(int i=0 ; i<l.size() ; i++)
            {
                System.out.println(l.get(i));
            }
        }
    }
    I wrote this in only a couple of minutes. Could be not the most efficient/dynamic way to do it but it should serve the purpose for now ;)
    Most of the code is only for instant testing for you.
     
  5. Offline

    Mike111177

    this worked very well thank you
     
  6. Offline

    Tempelchat

    No problem! Glad I could help you. :)
     
Thread Status:
Not open for further replies.

Share This Page