prabhu
prabhu

Reputation: 89

How to add a String to another String array in android?

This my java code, here i have to add a string "RFID" in a String Array itemtypes and i have to stored it in an another string array item.But im getting an error.

  String[] itemtype;
  String[] item;
     \...........
      .........../
   try {
                response = (SoapPrimitive) envelope.getResponse();
                Koradcnos = response.toString();
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            itemtypes = Koradcnos.split(";");
            item=itemtypes+"RFID";//here im getting error
        } catch (Exception er) {
            //Helper.warning("Error", er.toString(),this);
        }
         imageId = new int[itemtypes.length];
        for (int i = 0; i < itemtypes.length; i++)
            if (itemtypes[i].equals("Yarn")) {
                imageId[i] = R.drawable.yarnitem;

Upvotes: 0

Views: 97

Answers (3)

Viet
Viet

Reputation: 3409

You need to work around for this :

Using with Arrays.copyOf

item =  Arrays.copyOf(itemtypes , itemtypes.length + 1);
item[item.length - 1] = "RFID";

Or direct split from Koradcnos

item = (Koradcnos + ";RFID").split(";");

Upvotes: 1

starkshang
starkshang

Reputation: 8528

itemtypes is String array,it can't be modified,if you want to get a new array adding another element,you can use System.arrayCopy

System.arraycopy(Object src,  
             int  srcPos,
             Object dest, 
             int destPos,
             int length)

Like this:

item = new String[itemtypes.length+1];
System.arraycopy(itemtypes,0,item,0,itemtypes.length);
item[itemtypes.length] = "RFID";

Upvotes: 0

TeChNo_DeViL
TeChNo_DeViL

Reputation: 739

You cannot concatenate string with a list String.split() method returns a String array.

Instead of item=itemtypes+"RFID" iterate through the array as:

for (int i=0; i < itemtypes.length();  i++) {
    item[i] = itemtypes[i] + "RFID";
}

Also I think the variable name will be itemtypes instead of itemtype

Upvotes: 0

Related Questions