RaagaSudha
RaagaSudha

Reputation: 397

How to store the values dynamically in string in java android?

In my project I need to store the values dynamically in a string and need to split that string with ",". How can I do that ? Please help me..

My Code:

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1; 


    for(int q=0;q<listhere.size();q++)
                {
                  arropids = listhere.get(q);

                  if(arropids.get(3).equals("1"))
                  {
                      arropids1 += arropids.get(0) + ","; 


                  System.out.println("arropids1"+arropids1);

                }
                } 

Upvotes: 0

Views: 1400

Answers (2)

Zaid Daghestani
Zaid Daghestani

Reputation: 8615

In order to split the results after storing your parse in the for loop, you use the split method on your stored string and set that equal to a string array like this:

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1 = ""; 


for(int q=0;q<listhere.size();q++) {
              arropids = listhere.get(q);

              if(arropids.get(3).equals("1"))
              {
                  arropids1 += arropids.get(0) + ","; 


              System.out.println("arropids1"+arropids1);

              }
      }
      String[] results = arropids1.split(",");
      for (int i =0; i < results.length; i++) {
           System.out.println(results[i]);
      }

I hope that this is what you're looking for.

Upvotes: 0

jeet
jeet

Reputation: 29199

You must be getting NullPointerException as you havent initialized the String, initialize it as

String arropids1="";

It will resolve your issue, but I dont Recommend String for this task, as String is Immutable type, you can use StringBuffer for this purpose, so I recommend following code:

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;

StringBuffer buffer=new StringBuffer();

    for(int q=0;q<listhere.size();q++)
                {
                  arropids = listhere.get(q);

                  if(arropids.get(3).equals("1"))
                  {
                      buffer.append(arropids.get(0));
                      buffer.append(","); 


                  System.out.println("arropids1"+arropids1);

                }
                }

and finally get String from that buffer by:

 String arropids1=buffer.toString(); 

Upvotes: 2

Related Questions