0

Here is the code to put values in ArrayList and I am unable to split the arraylist with ",". Can someone please help me as to how to achieve this task ?

           spinnerArrayList = new ArrayList<String>();
           spinnerArrayList.add(menuFieldInstance.getFieldValues());
           Log.i("spinnerArrayList",""+spinnerArrayList);  
          //for(int j=0;j<spinnerArrayList.size();j++)
          //{
          Log.i("spinnerArrayList after splitting ,",""+spinnerArrayList.get(0).split(","));
           //} 

Here is the Logcat of Spinner ArrayList and SpinnerArrayList after splitting.............

02-10 22:00:48.285: I/spinnerArrayList(19378): [0100~Avon & Somerset,0200~Bedfordshire,0300~Cambridgeshire,0400~Cheshire,0500~City of London,0600~Cleveland,0700~Cumbria,0800~Derbyshire,0900~Devon & Cornwall,1000~Dorset,1100~Durham,1200~Essex,1300~Gloucestershire,1400~Greater Manchester,1500~Hampshire,1600~Hertfordshire,1700~Humberside,1800~Kent,1900~Lancashire,2000~Leicestershire,2100~Linconshire,2200~Merseyside,2300~Metropolitan,2400~Norfolk,2500~Northamptonshire,2600~Northumbria,2700~North Yorkshire,2800~Nottinghamshire,2900~South Yorkshire,3000~Staffordshire,3100~Suffolk,3200~Surrey,3300~Sussex,3400~Thames Valley,3500~Warwickshire,3600~West Mercia,3700~West Midlands,3800~West Yorkshire,3900~Wiltshire,4000~Dyfed,4100~Gwent,4200~North Wales,4300~South Wales,4400~Royal Ulster,4500~Strathclyde,4600~Central Scotland,4700~Dumfries and Galloway,4800~Fife,4900~Grampian,5000~Lothian and Borders,5100~Northern Scotland,5200~Tayside,5300~Gurnsey,5400~States of Jersey,5500~Isle of Man,NO~No Police Response,THAM~THAMES VALLEY,WEST~WEST MIDLANDS POLICE,5600~Buckinghamshire] 02-10 22:00:48.285: I/spinnerArrayList after splitting ,(19378): [Ljava.lang.String;@41b9a498

4
  • split return one array of String, so if you want get value of that you need log one index of that not log all array like this. Commented Feb 10, 2014 at 6:19
  • can u give me sample code or edit my code Commented Feb 10, 2014 at 6:21
  • 1
    Arraylist contains discrete elements, you don't "split" it, it is already "splitted". Just use get() with an index. Commented Feb 10, 2014 at 6:22
  • spinnerArrayList.get(0).split(",")[i] where i is int position in array Commented Feb 10, 2014 at 6:24

3 Answers 3

2
// try to print this way then you getting actual value at index becz your try to print String[] object rather each index value so do this way
spinnerArrayList = new ArrayList<String>();
spinnerArrayList.add("");
for (int i=0;i<spinnerArrayList.size();i++){
        String[] splitedValue = spinnerArrayList.get(i).split(",");
        for (int j=0;j<splitedValue.length;j++){
            Log.i(i+" at ArrayIndex "+j+" at splitedIndex Value is >> ",splitedValue[j]);
            String[] splitedValue1 = splitedValue[j].split("~");
            if(splitedValue1.length==1){
                continue;
            }
            for (int k=0;k<splitedValue1.length;k++){
                Log.i(j+" at splitedIndex "+k+" at splited1Index Value is >> ",splitedValue1[k]);
            }
        }

    }
Sign up to request clarification or add additional context in comments.

5 Comments

can u please help me i want to split the "~" in splitedValue[j] but i am failed to do so ..... i have written Log.i(j+" at ArrayIndex "+k+" at splitedIndex Value is >> ",splitedValue[k].split("~"));
i have edit it pls review the code and why u have put if(splitedValue1.length ==1){ continue; }
Oky try to comment this code and see out put difference then you getting why should i put it...
can you give me your text which ever you want split ?
1

You can't split ArrayList. You can split String, e.g "I am some String, you can split me".split(",") will return an array of 2 Strings, but ArrayList is a data structure which holds some Strings and it doesn't mean that they are separated with comma. You can try to split each item of the list, e.g.

for (String s : spinnerArrayList) {
    String[] res = s.split(",");
    // do smth with res
}

Comments

0

spinnerArrayList.get(0).split(",") doesn't split "the arrayList" it splits the string at index zero, and since the returned result is an array of strings - you can't concatenate it using the + operator to another string:

Log.i("spinnerArrayList after splitting ,",""+spinnerArrayList.get(0).split(","));

Read the splitted string into a String array first:

String[] arr = spinnerArrayList.get(0).split(",");

and then loop the array and print the values"

for(String val: arr){
    Log.i(val);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.