Arraylist help


Last Updated:

  1. Jacob Malliet

    Jacob Malliet Active Member This Topic's Starter

    Joined:
    Jun 13, 2010
    Messages:
    29
    Likes Received:
    1
    Hello, I am stuck on figuring out how to name an array list based on user input.

    For example, if i have an edit text where the user can input a label. How would i create an array list named with what ever was in the edit text?

    Thanks in advance
     

    Advertisement
  2. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    I'm not sure why you'd want to? The user doesn't care what the list is named. Do you mean that you want to add the value of an edit text box to the list?
     
  3. Jacob Malliet

    Jacob Malliet Active Member This Topic's Starter

    Joined:
    Jun 13, 2010
    Messages:
    29
    Likes Received:
    1
    Well, I need multiple categories and in each category which the user creates, I need an arraylist of objects. Would i need to create a set amount to begin with? or is there a way to create these arraylists during runtime?
     
  4. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    You can always make a new arraylist while the program is running. I guess you could have an array of arraylists that you add to?

    Are you sure you don't want to use a database?
     
  5. Jacob Malliet

    Jacob Malliet Active Member This Topic's Starter

    Joined:
    Jun 13, 2010
    Messages:
    29
    Likes Received:
    1
    possibly, I'm not sure how to use a database though. Can you point me to an article explaining how to use them? Sorry, I'm still new to developing
     
  6. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    The notepad example on android developers is a good starting place. Basically if I understand what you want, your user will type in category names and then fill in items in that category?
     
  7. Jacob Malliet

    Jacob Malliet Active Member This Topic's Starter

    Joined:
    Jun 13, 2010
    Messages:
    29
    Likes Received:
    1
    Yea ill be sure to check that out, thanks for the help.
     
  8. wata

    wata Member

    Joined:
    Jun 23, 2010
    Messages:
    9
    Likes Received:
    1
    I'm sure there are multiple approaches to this problem, but this is what I would do (from a purely Java approach):

    Solution
    1. Create a class called Category (or name it what ever you like, but Category seems appropriate)
    2. Have Category extend ArrayList (so now Category is an ArrayList but your about to make it much more than just an ArrayList)
    3. In the constructor of Category, you will have a String parameter called name and a String field called name, you will store the argument into the field once the object is created
    4. You will have a method called getName (which returns the field called name)
    5. In your main class (the class in which you handle and listen for actions in), you will create an ArrayList of the class Category called categories
    6. Each time the user creates a new category you will add that category to the ArrayList categories with the user specified name of the category
    7. Done.

    So something like this:
    PHP:
    1.  
    2. //YOUR MAIN CLASS - your class that handles and listens for actions
    3. class Main{
    4. ArrayList<Category> categories = new ArrayList<Category>();
    5.  
    6. //...
    7. //when user enters category information
    8. categories.add(new Category(tv.getText()+"");
    9. //...
    10.  
    11. }
    12.  
    13. //CATEGORY CLASS - a class that store each category
    14. class Category extends ArrayList{
    15. private String name;
    16.  
    17. Category(String name){
    18. this.name = name;
    19. }
    20.  
    21. String getName(){
    22. return name;
    23. }
    24. }
    25.  
    Wata
     
    Jacob Malliet likes this.
  9. balainfant

    balainfant Member

    Joined:
    Jun 18, 2010
    Messages:
    20
    Likes Received:
    0
    hi please help me. ho to write code mobile lock in android please give idea and example code thanks for advance
     
  10. wata

    wata Member

    Joined:
    Jun 23, 2010
    Messages:
    9
    Likes Received:
    1
    Although, I'm not a moderator here, I will ask you to refrain from asking off topic questions in multiple threads. Please create a new thread for your question and wait patiently for an answer. Asking questions in threads that do not have any association with your question won't help in receiving answers. As well, proper sentence structure would help others understand your question better; just a tip.

    See his posts: http://androidforums.com/search.php?searchid=2045255
     
  11. Jacob Malliet

    Jacob Malliet Active Member This Topic's Starter

    Joined:
    Jun 13, 2010
    Messages:
    29
    Likes Received:
    1
    Thanks, This is exactly what i needed
     

Share This Page

Loading...