嗨我正在从千克转换成石头磅,我想将石头放在一个edittext中,然后在一个edittext中放置,下面是我的方法
public ArrayList<HashMap<String, Integer>> kiloTostomepound(int s) {
int stone = (int) (s * 2.2);
int stonevalue = stone/14;
int spound = (stone % 14);
arrayList = new ArrayList<HashMap<String,Integer>>();
HashMap<String, Integer> h1 = new HashMap<String, Integer>();
h1.put(STONE,stonevalue);
h1.put(STONEPOUND, spound);
arrayList.add(h1);
return arrayList;
}
我想从arryalist获取值并将其设置为edittext。
执行以下但givinf nullpointerexception
edt2.setText(String.valueOf(kiloTostomepound(arrayList.get(0).get(STONE))));
edt3.setText(String.valueOf(kiloTostomepound(arrayList.get(1).get(STONEPOUND))));
答案 0 :(得分:0)
以下代码遍历列表并打印键和值。
for (int a =0; a<myList.size();a++)
{
HashMap<String, Integer> tmpData = (HashMap<String, Integer>) myList.get(a);
Set<String> key = tmpData.keySet();
Iterator it = key.iterator();
while (it.hasNext()) {
String hmKey = (String)it.next();
Integer hmData = (Integer) tmpData.get(hmKey);
System.out.println("Key: "+hmKey +" & Data: "+hmData);
it.remove(); // avoids a ConcurrentModificationException
}
}
因此,如果您需要第一个值,请尝试以下操作。
HashMap<String, Integer> tmpData = (HashMap<String, Integer>) myList.get(0);
Set<String> key = tmpData.keySet();
Iterator it = key.iterator();
// Get the first element
String hmKey = (String)it.next();
Integer hmData = (Integer) tmpData.get(hmKey);
// Set the key and value to Edit text
edt2.setText(hmKey+" "+hmData);
答案 1 :(得分:0)
似乎就像将值赋给方法内的arrayList变量一样。因此
kiloTostomepound(arrayList.get(0).get(STONE))
将为arrayList抛出一个nullpointer。
如果我正确理解您的代码,下面的代码应该可以解决问题。但我不知道kiloTostomepound方法的输入。
edt2.setText(String.valueOf(kiloTostomepound("INPUT TO METHOD").get(0).get(STONE)));
edt3.setText(String.valueOf(kiloTostomepound("INPUT TO METHOD").get(1).get(STONEPOUND)));
这是因为你的方法返回带有井号内的arrayList。