How to get a value of an arrayList in Android?


I have the following ArrayList which filled with information from a database

ArrayList<HashMap<String, String>> employeeList;
employeeList = new ArrayList<>();
private static final String TAG_NAME = "name";
public static final String TAG_DESIGNATION = "designation";

Log.d("INICIA ","employeeList= "+employeeList);

In Logcat I get the following:

D/INICIA: employeeList= [{designation=manager, name=rick}]

Everything works fine but I have a doubt how I could access the value manager that is in the arrayList so I can use it in a condition

asked by El Cóndor 03.03.2017 в 03:22

2 answers


A. If there is a single pair of values

Simply obtain that value by the name of your key in the HashMap that you have created. The get (0) looks for the first (and in this case only value) within the map:

String sDesignation= employeeList.get(0).get("designation");

The variable sDesignation will have the value manager in this case.

B. If there are several pairs of values on the map

If there are several pairs of values in the array, reading is usually done within a loop (for, while ...).

Example with Iterator and While :

 *Este ejemplo obtiene, en el caso de que haya varios valores, 
 *en la variable 'key' la llave (designation, name...) 
 *y en la variable 'value' los valores de cada llave (manager, rick...)

Iterator myIterator = employeeList.keySet().iterator();
while(myIterator.hasNext()) {
    String key=(String);
    String value=(String)employeeList.get(key);

Example with for loop

for (HashMap<String, String> map : employeeList)
     for (Entry<String, String> mapEntry : map.entrySet())
        String key = mapEntry.getKey();
        String value = mapEntry.getValue();
answered by 03.03.2017 / 03:59

According to your ArrayList, it contains a HashMap with keys designation and name :

ArrayList<HashMap<String, String>> employeeList;

You can get the values using the index

for (int i = 0; i < employeeList.size(); i ++) {
   Log.d("INICIA ","designation= "+employeeList.get(i).get("designation"));  
   Log.d("INICIA ","name= "+employeeList.get(i).get("name"));          
answered by 03.03.2017 в 04:14