How to combine two array list and show in a listview in android

前端 未结 3 1431
北海茫月
北海茫月 2020-12-03 05:23

I want to combine two ArrayList into a single one.

My first arraylist looks like this:

{a,s,d,f,g,h,......}

My second arraylist lo

3条回答
  •  旧时难觅i
    2020-12-03 05:51

    if both the List (like List and List) have different data types

    List stringsList= new ArrayList<>();
    stringsList.add("A string");
    stringsList.add("another string");
    stringsList.add("and one more");
    
    List integersList = new ArrayList<>();
    integersList.add(1337);
    integersList.add(1338);
    integersList.add(1339);
    
    // New list containing all the items form
    // stringsList and integersList
    List allIWeHave= new ArrayList<>();
    allIWeHave.addAll(stringsList);
    allIWeHave.addAll(integersList);
    
    //while fetching you check either item is Integer or String
    if(allIWeHave.get(0) instanceof Integer){
    //Integer value
    }else{
    //String value
    }
    
        

    提交回复
    热议问题