字符串到BigDecimal

时间:2014-10-05 15:47:41

标签: java android database bigdecimal searchview

抱歉我的英语! 我有BigDecimal的问题。 我创建了一个SQLiteDatabase。现在我想用搜索视图进行搜索。

private void showResults(String query) {

    Cursor cursor = mDbHelper.searchCustomer((query != null ? query.toString() : "@@@@.@@"));

    if (cursor == null) {
        //
    } else {
        // Specify the columns we want to display in the result
        String[] from = new String[] { 
                ItemDbAdapter.KEY_1,
                ItemDbAdapter.KEY_2,
                ItemDbAdapter.KEY_3,
                ItemDbAdapter.KEY_4, 
                ItemDbAdapter.KEY_5,
                ItemDbAdapter.KEY_6,
                ItemDbAdapter.KEY_7,
                ItemDbAdapter.KEY_8};

        // Specify the Corresponding layout elements where we want the
        // columns to go
        int[] to = new int[] { 
                R.id.result_1, 
                R.id.result_2, 
                R.id.result_3,
                R.id.result_4, 
                R.id.result_5, 
                R.id.result_6, 
                R.id.result_7, 
                R.id.result_8};

        SimpleCursorAdapter customers = new SimpleCursorAdapter(this,
                R.layout.customresult, cursor, from, to);
        mListView.setAdapter(customers);

        mListView.setOnItemClickListener(new OnItemClickListener() {                
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                np.setValue(100); //Numberpicker


                Cursor cursor = (Cursor) mListView.getItemAtPosition(position);                 


                String 1 = cursor.getString(cursor.getColumnIndexOrThrow("1"));
                String 2 = cursor.getString(cursor.getColumnIndexOrThrow("2"));
                String 3 = cursor.getString(cursor.getColumnIndexOrThrow("3"));
                String 4 = cursor.getString(cursor.getColumnIndexOrThrow("4"));
                String 5 = cursor.getString(cursor.getColumnIndexOrThrow("5"));
                String 6 = cursor.getString(cursor.getColumnIndexOrThrow("6"));
                String 7 = cursor.getString(cursor.getColumnIndexOrThrow("7"));
                String 8 = cursor.getString(cursor.getColumnIndexOrThrow("8"));


                RelativeLayout customerLayout = (RelativeLayout) findViewById(R.id.custominfoLayout);
                if (customerLayout == null) {
                    // Inflate the Customer Information View
                    RelativeLayout leftLayout = (RelativeLayout) findViewById(R.id.rightLayout);
                    View customerInfo = getLayoutInflater().inflate(
                            R.layout.custominfo, leftLayout, false);
                    leftLayout.addView(customerInfo);
                }


                Tv1 = (TextView) findViewById(R.id.info_i);
                Tv2 = (TextView) findViewById(R.id.info_a);
                Tv3 = (TextView) findViewById(R.id.info_k);
                Tv4 = (TextView) findViewById(R.id.info_B);
                Tv5 = (TextView) findViewById(R.id.info_k);
                Tv6 = (TextView) findViewById(R.id.info_k2);
                Tv7 = (TextView) findViewById(R.id.info_P);
                Tv8 = (TextView) findViewById(R.id.info_F);


                Tv1.setText(1);
                Tv2.setText(2);
                Tv3.setText(3);
                Tv4.setText(4);
                Tv5.setText(5);
                Tv6.setText(6);
                Tv7.setText(7);
                Tv8.setText(8);

                searchView.setQuery("", true);

直到这里它运作良好。我所寻求的是什么。

如果我想将值存储在BigDecimal中,我会收到以下错误消息:

  

10-05 17:58:51.759:E / AndroidRuntime(28053):致命异常:主要   10-05 17:58:51.759:E / AndroidRuntime(28053):进程:com.XXX.XXXX,PID:28053   10-05 17:58:51.759:E / AndroidRuntime(28053):java.lang.NumberFormatException:无效长:&#34; 0,3&#34;   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.lang.Long.invalidLong(Long.java:124)   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.lang.Long.parse(Long.java:361)   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.lang.Long.parseLong(Long.java:352)   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.lang.Long.parseLong(Long.java:318)   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.math.BigDecimal。(BigDecimal.java:344)   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.math.BigDecimal。(BigDecimal.java:425)   10-05 17:58:51.759:E / AndroidRuntime(28053):at com.XXX.XXXX.ListeActivity $ 1.onItemClick(ListeActivity.java:288)

     

10-05 17:58:51.759:E / AndroidRuntime(28053):在android.widget.AdapterView.performItemClick(AdapterView.java:299)   10-05 17:58:51.759:E / AndroidRuntime(28053):在android.widget.AbsListView.performItemClick(AbsListView.java:1113)   10-05 17:58:51.759:E / AndroidRuntime(28053):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2911)   10-05 17:58:51.759:E / AndroidRuntime(28053):在android.widget.AbsListView $ 3.run(AbsListView.java:3645)   10-05 17:58:51.759:E / AndroidRuntime(28053):在android.os.Handler.handleCallback(Handler.java:733)   10-05 17:58:51.759:E / AndroidRuntime(28053):在android.os.Handler.dispatchMessage(Handler.java:95)   10-05 17:58:51.759:E / AndroidRuntime(28053):在android.os.Looper.loop(Looper.java:136)   10-05 17:58:51.759:E / AndroidRuntime(28053):在android.app.ActivityThread.main(ActivityThread.java:5001)   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.lang.reflect.Method.invokeNative(Native Method)   10-05 17:58:51.759:E / AndroidRuntime(28053):at java.lang.reflect.Method.invoke(Method.java:515)   10-05 17:58:51.759:E / AndroidRuntime(28053):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)   10-05 17:58:51.759:E / AndroidRuntime(28053):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)   10-05 17:58:51.759:E / AndroidRuntime(28053):at dalvik.system.NativeStart.main(Native Method)

我的继续:

                Sa = String.valueOf(Tv1.getText());
                Sb = String.valueOf(Tv2.getText());
                Sc = String.valueOf(Tv3.getText());
                Sd = String.valueOf(Tv4.getText());
                Se = String.valueOf(Tv5.getText());
                Sf = String.valueOf(Tv6.getText());
                Sg = String.valueOf(Tv7.getText());




                Big1 = new BigDecimal(Sa);
                Big2 = new BigDecimal(Sb);
                Big3 = new BigDecimal(Sc);
                Big4 = new BigDecimal(Sd);
                Big5 = new BigDecimal(Se);
                Big6 = new BigDecimal(Sf);
                Big7 = new BigDecimal(Sg);

我不明白为什么会出现这个错误。

我只使用String和BigDecimal,但错误是浮动。

2 个答案:

答案 0 :(得分:1)

java variable names cant be just numbers, or even start with a number

String 1 = cursor.getString(cursor.getColumnIndexOrThrow("1"));
String 2 = cursor.getString(cursor.getColumnIndexOrThrow("2"));
....

稍后你设置

Tv1.setText(1);
Tv2.setText(2);

构建应该给你警告。

答案 1 :(得分:0)

我想知道变量1, 2, 3, 4, 5, ...的名称是否是有效的变量名。我建议使用firstColumnTextArrayList等变量名称。因此,您遇到的错误可能会受此影响。

您还声称显示的错误为float,但绝对不是! 0,3可能是逗号分隔的BigDecimals或其他内容列表!

根据您提供的信息,我无法对此问题提出任何建议,但可以通过使用默认的Android样式配置等 checkstyle 等工具来避免此类问题。