Java 如何从该arraylist获取值并显示包含该值的文本?
我为一个清单找到了这个设置,但是我如何才能获得清单中的内容列表以显示给用户,以及如果用户丢弃或使用某个项目,我如何删除该项目?最后,将每个项目与包含其统计信息的类关联的最佳方法是什么Java 如何从该arraylist获取值并显示包含该值的文本?,java,android,arraylist,andengine,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
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;
}
包示例;
导入java.util.ArrayList;
导入java.util.HashMap;
公共班机{
//具有按ID列出的项的ArrayList的HashMap。
public static HashMap items=new HashMap();
//结构稍有不同的背包。
public static HashMap backup=new HashMap();
公共静态void main(字符串[]args){
//创建阵列列表。
ArrayList boots=新的ArrayList();
ArrayList刀剑=新的ArrayList();
ArrayList rings=新的ArrayList();
ArrayList daggers=新的ArrayList();
ArrayList bows=新的ArrayList();
//为它们添加内容。。。
对于(整数i=0;i<10;i++){
boots.add(“Boot”+i.toString());
添加(“剑”+i.toString());
添加(“Ring”+i.toString());
匕首。添加(“匕首”+i.toString());
添加(“Bow”+i.toString());
}
//将ArrayList按其ID放入项的HashMap。
物品.放置(ID.靴子,靴子);物品.放置(ID.剑,剑);
物品.放置(ID.戒指,戒指);物品.放置(ID.匕首,匕首);
物品。放置(ID.弓、弓);
//打印所有ID中所有ArrayList中的所有内容。
对于(ArrayList e:items.values()){
对于(int i=0;i
}我假设HashMap、Item对应于库存,backpack对应于用户。您可以做的最简单的事情是使用一个表示项的超类或接口并保持计数。如果您可以继承它以用于每个项目,并处理它的计数,那么它将非常有用。类似地,使用一个类来表示库存,其中包含相应的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;
}
}
公共类目录{
HashMap itemMap;//表示库存中的所有项目
公共void setItemMap(HashMap itemMap){
this.itemMap=itemMap;
}
}
如果创建此库存类的对象并将项目编号和相应的项目添加到映射中,则变量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.
public Inventory initialiseInventory(){
存货清单OBJ=新存货();
HashMap itemMap=新的HashMap;
剑剑=新剑();
环SOBj=新环();
swordObj.setInitialStock(100);//放置100把剑
ringsObj.setInitialStcok(200);//200个环就位
物品地图。放置(ID.SWORS,swordObj);
itemMap.put(ID.RINGS,ringsObj);
inventoryObj.setItemMap(itemMap);
返回inventoryObj;
}
公共视图资源清册(资源清册){
对于(条目itemEntry:inventory.itemMap.entrySet()){
ItemItemObj=itemEntry.getValue();
System.out.println(itemObj.getItemName());
System.out.println(“-------------------------------------”);
System.out.println(itemObj.getInitialStock());
System.out.println(itemObj.getCurrentStock());
}
}
现在,您可以表示一个项目的删除,您可以减少该特定项目的计数。这是一个骨架,你可以改进它。
我认为如果为每个项目创建类,也可以添加一个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);
}
公共类目录{
Item boots=新项目(101,“boots”,5,4);\\101是id
项目剑。。。。
ArrayList stock=新的ArrayList();
添加(靴子);
}
这只是一个开始。我已经包括了一些关于ArrayList和HashMaps的教程,这可能也会有所帮助
-集合和阵列列表
-HashMap不要对泛型类型使用太多的Arraylist,而是创建一个Pojo类,其中包含清单的setter和getter。它将减少应用程序的权重,并可以使用适配器类在listview中显示。从listview中,我们可以通过获取位置来删除项目,那么我该如何调用以查看库存中的项目,或者调用(比如)插槽1?以及如何调用纹理,或者基于纹理创建新对象?