How to view all items in java arraylist?

In my Android application, I want to be able to add a string value to a static arraylist, which I declared in my main android activity. The logic is as follows:

1) When you press the button and the action begins. In the oncreate method, I want to save the class name, which is the current activity, for a string value. For instance:

String className = "com.lunchlist.demo"; 

After this value is assigned, I want to immediately add this string value to the Static ArrayList, which I declared in my main android activity (which means the start of the first Android activity) After adding the value

I did something like this:

 static String List<String> members = new ArrayList<String>(); 

This is announced in my main activity. Now, when I click the button to start another action, I use this to add the string class name for this current activity to my arraylist in my oncreate method:

  String className = "com.lunchlist.demo" members.add(className); 

Now my question is: will this add the string value to my arraylist and save it for later use? For example, if I press three different buttons, this will add three className values ​​to the arraylist. Will it then store a string value that will contain three different string values ​​for my arraylist member? How can I check each element in my array to see if values ​​are added when a new action starts?

I ask about this because I will need to get this and save these values ​​using general settings, then restore them and start using, using the string value, which is the class to trigger the activity. I got activity starting with the string value of the class name. I just have problems saving them.

+4
source share
3 answers

Answering all your questions:

will this add a string value to my arraylist and save it for later use?

Yes. Your code seems perfect for this without a problem.

For example, if I press three different buttons, this will add three different values ​​for the Name class for arraylist. Will it store a String value that will contain three different string values ​​for my arraylist members?

If you tell your onClickListener button onClickListener add a row to ArrayList members, then this will be done, and regardless of whether you have already added this member to ArrayList, because the lists of arrays do not care if data is duplicated or not.

How can I check each element in my array to find out if the values ​​match are added when a new action starts?

You must iterate the list of arrays with or for each cicle, and then print this member name as a log entry.

  • For each cell

     for (String member : members){ Log.i("Member name: ", member); } 
  • Simple for cicle

     int listSize = members.size(); for (int i = 0; i<listSize; i++){ Log.i("Member name: ", members.get(i)); } 

If you try to print / write down a value whose value is out of range, i.e. i < 0 || i >= listSize i < 0 || i >= listSize , an IndexOutOfBoundsException will be thrown and your application will fail.

+17
source

For-Every iteration introduced in Java with Java 1.5:

 for (String s : members){ Log.d("My array list content: ", s); } 

See this link for more details:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

+6
source

Try it, its lights me :)

 ArrayList<String> limits; limits = new ArrayList<String>(); for (String s : limits) { Toast.makeText(getApplicationContext(),s , Toast.LENGTH_SHORT).show(); } 
+2
source

All Articles