如何从此arraylist获取值并显示包含它的文本?

时间:2014-10-17 03:23:24

标签: java android arraylist andengine

我发现了这个库存设置,但是如何获取清单中的清单以显示用户,如何删除或使用该项目?最后,将每个项目与包含其统计信息的类相关联的最佳方法是什么?

package example;

import java.util.ArrayList;
import java.util.HashMap;

public class Main {

// HashMap with arrayLists of items by ID.
public static HashMap<Integer, ArrayList<String>> items = new HashMap<Integer, ArrayList<String>>();
// Backpack with a slightly different structure.
public static HashMap<Integer, String> backpack = new HashMap<Integer, String>();

public static void main(String[] args) {

    // Creating the arrayLists.
    ArrayList<String> boots = new ArrayList<String>();
    ArrayList<String> swords = new ArrayList<String>();
    ArrayList<String> rings = new ArrayList<String>();
    ArrayList<String> daggers = new ArrayList<String>();
    ArrayList<String> bows = new ArrayList<String>();

    // Adding content to them...
    for (Integer i = 0; i < 10; i++) {
        boots.add("Boot" + i.toString());
        swords.add("Sword" + i.toString());
        rings.add("Ring" + i.toString());
        daggers.add("Dagger" + i.toString());
        bows.add("Bow" + i.toString());
    }

    // Putting the arrayLists to the HashMap of items by their IDs.
    items.put(ID.BOOTS, boots); items.put(ID.SWORDS, swords);
    items.put(ID.RINGS, rings); items.put(ID.DAGGERS, daggers);
    items.put(ID.BOWS, bows);

    // Printing all the content in all arrayLists in all IDs.
    for (ArrayList<String> e : items.values()) {
        for (int i = 0; i < e.size(); i++) {
            if (e.get(i)!=null) {
                System.out.println(e.get(i));
            }
        }
    }

    // Here you could add any item to the backpack.
    // backpack.put(ID.SWORDS, swords.get(0));
}

public class ID {

    //Defining constant-IDs.
    public static final int BOOTS = 0;
    public static final int SWORDS = 1;
    public static final int RINGS = 2;
    public static final int DAGGERS = 3;
    public static final int BOWS = 4;

}

}

3 个答案:

答案 0 :(得分:1)

我假设HashMap,Item对应于库存,背包对应于用户。你能做的最简单的事情就是拥有一个代表项目的超类或接口并保持计数。如果您可以继承它以用于每个项目,并处理它的计数,那将是有用的。同样,有一个用于表示库存的类,其中包含相应的项子类和对象。

这样的东西适用于Item,

 public class Item
 {
        String itemName;
        int initialStock;
        int currentStock;

    public int getInitialStock(){

         return initialStock();

    }

    public void setInitialStock(int initialStock){

             this.initialStock=initialStock;

   }
        //Other getters and setters too.
 }


 public class Sword extends Item{

      // You can use Item's methods for the statistics calculation.



 }

对于库存,

  public  class Inventory{

    HashMap<Integer,Item> itemMap; // To represent all items in inventory

    public void setItemMap(HashMap<Integer,Item> itemMap){

             this.itemMap=itemMap;
    }

  }

如果您创建此Inventory类的对象并将项目编号和相应项添加到地图,则变量itemMap表示所有项目的库存。

例如,

    public Inventory initialiseInventory(){
       Inventory inventoryObj=new Inventory();
       HashMap<Integer,Item> itemMap=new HashMap<Integer,Item>;


       Sword swordObj=new Sword();
       Rings ringsObj=new Rings();


       swordObj.setInitialStock(100);//100 swords in place
       ringsObj.setInitialStcok(200);//200 rings in place


       itemMap.put(ID.SWORDS,swordObj);
       itemMap.put(ID.RINGS,ringsObj);

       inventoryObj.setItemMap(itemMap);

       return inventoryObj;
    }


    public viewInventory(Inventory inventory){

    for(Entry<Integer,Item> itemEntry:inventory.itemMap.entrySet()){

           Item itemObj=itemEntry.getValue();
           System.out.println(itemObj.getItemName());
           System.out.println("---------------------------------");
           System.out.println(itemObj.getInitialStock());
           System.out.println(itemObj.getCurrentStock());

        }
    }

   Now, you can to represent removal of an item, you can reduce the count for that particular item. This is a skeleton, you can improve on it.

答案 1 :(得分:1)

不要使用太多具有泛型类型的Arraylist,而是为库存创建一个带有setter和getter的Pojo类。它将减少应用程序权重,并可使用适配器类在列表视图中显示。从列表视图中我们可以通过获取位置来删除项目

答案 2 :(得分:0)

我认为如果你为每个项目创建类,你可以添加一个ID字段,然后你就不需要HashMap了。

public class Inventory{

   Item boots = new Item(101, "boots", 5, 4); \\ 101 is the id
   Item swords ....

   ArrayList<Item> stock = new ArrayList<Item>();
   stock.add(boots);  


} 

这只是一个开始。我已经在ArrayLists和HashMaps中加入了一些可能也有帮助的教程。

https://www.youtube.com/watch?v=jU5ACV5MucM - 收藏品&amp;数组列表 https://www.youtube.com/watch?v=-JOSjIan2g0 - HashMap

相关问题