2015-01-06 47 views
-1

你好我是从千到石磅,我想放在一个EditText上的石头和英镑在一个EditText上转换,下面是我的方法安卓:设置的EditText与arraylistvalue

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

你有多个edittext或单个吗? –

+0

@RemeesMSyde我有多个edittexts – teekib

回答

0

下面的代码通过遍历该列表并打印键和值。

​​

所以,如果你需要第一个值试下面。

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); 
0

看起来像您将值分配给方法内部的arrayList变量。因此,

kiloTostomepound(arrayList.get(0).get(STONE)) 

将为arrayList抛出一个空指针。

如果我能正确理解你的代码,下面的代码应该可以做到。但是我不知道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与hashmaps里面。