2012-01-05 73 views
-1

它使我的應用程序讀取爲0字符串數組沒有小數?

<string-array name="metric_threads_per_inch"> 
    <item>0.5</item> 
    <item>0.8</item> 
</string-array> 

下面是代碼讀它

int[] thread = kit.getIntArray(R.array.metric_threads_per_inch); 
    adapter3 = new NumericAdapter(thread); 
+0

更多詳細信息請... – 2012-01-05 05:05:10

+0

你能告訴我們代碼是0嗎? – 2012-01-05 05:05:21

+0

請提供你如何閱讀這個XML文件的代碼。 – 2012-01-05 05:05:26

回答

0

我猜你是不正確解析字符串,使用類似:

final String[] sa = getResources().getStringArray(R.array.metric_threads_per_inch); 
final float f0 = Float.parseFloat(sa[0]); 
1

看來你在閱讀String-Array的方式有問題,從resources,這裏應該是怎麼回事

String string[] = new String[2]; 
string = getResources().getStringArray(R.array.metric_threads_per_inch); 
for (int i = 0; i < string.length; i++) { 
    Log.d("value", string[i]); 
} 

OUTPUT:

01-05 10:45:46.502: DEBUG/value(462): 0.5 
01-05 10:45:46.502: DEBUG/value(462): 0.8 
+1

suriji rocks ... – Richa 2012-01-05 05:49:40

0

你還沒有ü闡述如何訪問代碼...但你應該這樣讀它,因爲我曾經嘗試這樣做,並得到0.5和0.8的輸出

String[] mTestArray = getResources().getStringArray(R.array.metric_threads_per_inch); 

     for(int i=0;i<mTestArray.length;i++){ 
      Log.v("Log", "mTestArray : "+mTestArray[i]); 
     } 

希望它有幫助。

+0

謝謝你的所有幫助 – catalano 2012-01-05 05:32:09

0
int[] thread = kit.getIntArray(R.array.metric_threads_per_inch); 

您正在將字符串數組賦值給整數數組,然後如何獲得浮點值,如您在字符串數組中聲明的0.5,0.7。這些值將被四捨五入爲整數,所以你會得到零。

不要使用整數,看看這裏提供的許多答案來獲得正確的值。檢查它們。

0
Resources res = getResources(); 
String planets[] = res.getStringArray(R.array.sample); 
Float i = (Float)Float.parseFloat(planets[0].toString()); 
Toast.makeText(this,i.toString(),Toast.LENGTH_LONG).show(); 
+0

你必須採取浮法數組insare的Int – Sandy 2012-01-05 05:58:10

+0

上面的代碼爲我工作我的價值0.5 – Sandy 2012-01-05 05:58:53