简体   繁体   中英

how to equate items in the string-array android

i have two String arrays mylist and listprice.

 <string-array name="mylist">
    <item>Bottle</item>
    <item>Watch</item>
    <item>Books</item>
    <item>Mobile</item>
    <item>Purse</item>
    <item>Pen</item>
    <item>Glass</item>
    <item>Class</item>
    <item>Rubber</item>
    <item>Fan</item>
</string-array>

<string-array name="listprice">
    <item>160</item>
    <item>2600</item>
    <item>200</item>
    <item>26000</item>
    <item>260</item>
    <item>10</item>
    <item>500</item>
    <item>3000</item>
    <item>3</item>
    <item>380</item>
</string-array>

i want to initilize both the arrays items such that "bottle" equals "160","watch" equals "2600" and so on... how can i achieve it...thanks in advance...

You can use hash map for both array relation:

   HashMap<String,String> map=new HashMap<String,String>();
               for (int i = 0; i < getResources().getStringArray(R.array.mylist).length; i++) {
                    map.put(getResources().getStringArray(R.array.mylist)[i],getResources().getStringArray(R.array.listprice)[i]);
                }
String[] itemList = getResources().getStringArray(R.array.mylist);
String[] itemPrice = getResources().getStringArray(R.array.listprice);

Now you can put all these in hashmap.

HashMap<String, String> items = new HashMap<>();
for(int i = 0; i<itemList.length; i++){
items.put(itemList[i], itemPrice[i]);
}

Now if you want to get price of Fan then just write

String price = items.get("Fan"); //it will be 380

OR you can make use of str.equals(str2) method for checking the item and then from the index getting the price from itemPrice array.

You can get your array like this:

String[] listprice = getResources().getStringArray(R.array.listprice);
String[] mylist = getResources().getStringArray(R.array.mylist);

You can access string arrays with below code:

Resources res = getResources();
String[] mylist = res.getStringArray(R.array.mylist);
String[] listprice = res.getStringArray(R.array.listprice);

and you can use mylist[index] equals listprice[index]

Take two strings and concatinate the values .

 private String[] concat(String[] A, String[] B) {
       int aLen = A.length;
       int bLen = B.length;
       String[] C= new String[aLen+bLen];
       System.arraycopy(A, 0, C, 0, aLen);
       System.arraycopy(B, 0, C, aLen, bLen);
       return C;
    }

In java you can do something like with

String myList[]=getResources().getStringArray(R.array.mylist);
String myListPrice[]=getResources().getStringArray(R.array.listprice);

You can create a class lets say

public class ItemPrice{
      private String listItemName;
      private String listPrice;
      public String getListItemName(){return listItemName;}
      public String getListPrice(){return listPrice;}
      public void setListItemName(String itemName){this.listItemName = itemName;}
      public void setListPrice(String listPrice){this.listPrice = listPrice;}
}

Your array logic can be something like this

List<ItemPrice> itemPriceList = new ArrayList<ItemPrice>();
for(int i=0;i<myList.length;i++){
    ItemPrice items =  new ItemPrice();
    items.setListItemName(myList[i]);
    items.setListPrice(myListPrice[i]);
    itemPriceList.add(items);
}

So you can get the itemPriceList which will contain both item along with its price encapsulated in one object. I am assuming both array length will be same and are mapped according to their index.

Do share if this is what you are looking for and if you require further help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM