• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Arraylist help

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
 
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?
 
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?
 
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?
 
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
 
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?
 
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:
//YOUR MAIN CLASS - your class that handles and listens for actions
class Main{
ArrayList<Category> categories = new ArrayList<Category>();

//...
//when user enters category information
categories.add(new Category(tv.getText()+"");
//...

}

//CATEGORY CLASS - a class that store each category 
class Category extends ArrayList{
private String name;

Category(String name){
this.name = name;
}

String getName(){
return name;
}
}

Wata
 
hi please help me. ho to write code mobile lock in android please give idea and example code thanks for advance
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
 
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:
//YOUR MAIN CLASS - your class that handles and listens for actions
class Main{
ArrayList<Category> categories = new ArrayList<Category>();

//...
//when user enters category information
categories.add(new Category(tv.getText()+"");
//...

}

//CATEGORY CLASS - a class that store each category 
class Category extends ArrayList{
private String name;

Category(String name){
this.name = name;
}

String getName(){
return name;
}
}

Wata
Thanks, This is exactly what i needed
 
Back
Top Bottom