【问题标题】:Do calculations on numbers in 1 multiline textView and return result replacing numbers对 1 个多行 textView 中的数字进行计算并返回结果替换数字
【发布时间】:2016-01-12 10:48:19
【问题描述】:

我对 android 编程非常陌生,正在尝试完成我的第一个应用程序。它是一个配方转换器。

我已将我的食谱详细信息存储在 SQLite 数据库中,并且成分文本只是一个由回车符分隔的多行字符串。我使用光标将成分数据获取到一个文本视图中,该文本视图返回如下文本(可能是多种变体):

100ml 水 500 克 肉末 2 x 400g 罐装碎番茄

等等

我最初将每个数量、单位和成分描述分别存储在数据库中,这使转换时变得容易,但我选择将其存储在多行字符串中,以便从互联网或其他来源复制和粘贴成分。

我正在尝试提取数字,然后将它们乘以百分比,然后返回新转换的数字,以及相应的单位和描述,以获得如下结果:

(乘以 200%)

200ml 水 1000g 肉末 4 x 400g 罐装碎番茄

我只是不知道该怎么做。有人可以帮忙吗?

谢谢

更新:

我试图做这样的事情来获取数字。

公共无效拆分(){ TextView tvSplit = (TextView) findViewById(R.id.tvSplit);

    final TextView tvTest = (TextView) findViewById(R.id.tvTest);
    String s = tvTest.getText().toString();

    for (int i =0;i <= tvTest.getLineCount();i++){

        tvSplit.setText("");


        String text = s.replaceAll("\\D+", ",");
        tvSplit.append(text);
        tvSplit.append("\n");

    }

这向我显示了它们之间带有“,”的所有数字,但它还包括字符串中的所有数字,就像上面的示例一样,在转换之前它会显示 100,500,2,400,而我只需要 100,500,2。然后从那时起,我不确定如何将它们全部转换。我的“新编程思维”认为我可以通过 INSERT INTO tablename (id, originalvalues) VALUES(我的字符串,即 100,500,2)将这些存储在临时 SQL 表中。

然后我可以将它们拉出来,进行计算,更新表格,然后将它们与剩余的字符串一起添加回我的文本视图中。我还没有走那么远,所以我只是想知道正确的方法是什么。

更新 2:

根据我的 cmets,这是我用来显示警报对话框的代码,其中每个项目都列在单独的行上,然后我使用所选行查找任何“”之前的数字,然后在屏幕上显示文本.

public void PopUpSpinnerDialogue() {

    final TextView tvTest = (TextView) findViewById(R.id.tvTest);

    final TextView tv2 = (TextView) findViewById(R.id.tvTest2);

    String s = tvTest.getText().toString();


    final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
            R.layout.my_dropdown_style, s.split("\n"));



    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
    builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
            .setCancelable(false)
            .setAdapter(myAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    try {
                        String itemName = myAdapter.getItem(which).toString();

                        String[] parts = itemName.split(" ");

                        String itemNumStr = parts[0];

                        TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
                        EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
                        EditText et2 = (EditText) findViewById(R.id.etQtyHave);

                        tvLineName.setText(itemName);


                        String b4Space = itemNumStr.replaceAll("\\D+", "");
                        tv2.setText(b4Space);
                        et1.setText(b4Space);
                        et2.setText(b4Space);

                        calculateKeyIngredientPercent();
                    } catch (Exception e) {

                        Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
                    }

                }

            });
    android.app.AlertDialog alert = builder.create();
    alert.show();

}

我认为我可以使用这个想法,但我不知道如何对其进行编码然后显示结果。

更新 3:

我尝试使用的代码或至少是代码的想法是这样的。

TextView tvSplit = (TextView) findViewById(R.id.tvSplit);

    final TextView tvTest = (TextView) findViewById(R.id.tvTest);
    String s = tvTest.getText().toString();


    for (int i =0;i <= tvTest.getLineCount();i++){
    String[] ingreds = s.split("\n");
        tvSplit.setText("");

        String[] parts = ingreds.split(" ");

        String Qty = parts[0];
        String Units = parts[1];
        String Ingredients = parts[2];

        Integer QtyInt = Integer.parseInt(Qty);}

ingreds.split 不起作用,而且我不知道如何为每个 i 指定拆分部分。

【问题讨论】:

  • 请发布您已经尝试过的代码
  • 请看帖子中的更新。
  • 你在这里有个好东西 :) 我们需要更多信息:你发布了字符串'100ml Water 500 g Mince 2 x 400g can crushed tomato' - 你是这样解析的吗?或者你在 400 ang "g" 之间有空格?用户在任何时候都必须看到解析后的字符串还是只有在拆分后才会显示?
  • 我很抱歉我缺乏关于技术术语的知识,但无论用户在我的 EditText 中添加了什么,都会添加到我的 SQL 表中。这意味着用户可以放置一个空格,或者他们可能不会在 400 和“g”之间放置一个空格。我绝对希望他们在“g”和“成分描述”之间放一个空格,但我不知道如何强制它。但从逻辑上讲,当用户输入成分列表时,他们会在它们之间放置一个空格。如果我可以限制用户在 EditText 中输入的内容以强制在 Qty 和 Unit(400 和“g”)之间留一个空格,但如果不能,我必须假设他们也会这样做。
  • 所以 textView 显示的文本如“400g Water”+“\n”+“400 g Water”+“\n”。那有意义吗?此外,用户只需在转换后看到文本,然后我将其作为下拉列表添加到警报对话框中。然后,我将使用与此处相同的理论通过其他参数进一步转换它。希望我回答了你的问题?

标签: android sqlite textview


【解决方案1】:

我最终使用了正则表达式。它允许输入带有或不带有空格的数据。因此,我最终使用此代码提取每行的数量,将其乘以百分比,将文本(单位、成分描述)附加到该行,然后将其添加到字符串数组中,以添加到我的警报对话框中。

代码在这里。

public void Split() {

    final TextView tv2 = (TextView) findViewById(R.id.tvTest2);
    TextView tvTest = (TextView) findViewById(R.id.tvTest);
    TextView tvPercent = (TextView) findViewById(R.id.tvPercent);

    String tvP = tvPercent.getText().toString();
    String tvNumOnly = tvP.replaceAll("\\D+", "");
    Integer PercentVal = Integer.parseInt(tvNumOnly);
    String s = tvTest.getText().toString();

    StringBuilder sb = new StringBuilder();

    Pattern p = Pattern.compile("((\\d*) ?(.*)(?:\\n*))");
    Matcher m = p.matcher(s);

    while (m.find()) {

        String Qty = m.group(2) + ".00";
        String Ingred = m.group(3);

        Float QtyFloat = Float.parseFloat(Qty);

        Float newQTY = (QtyFloat * PercentVal) / 100;

        String newQTYstr = newQTY.toString();


        sb.append(newQTYstr + " " + Ingred + "\n");
    }

    String[] lines = sb.toString().split("\n");

    String[] IngredArray = Arrays.copyOfRange(lines, 0, lines.length - 1);

    final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
            R.layout.my_dropdown_style, IngredArray);


    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
    builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
            .setCancelable(false)
            .setAdapter(myAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    try {
                        String itemName = myAdapter.getItem(which).toString();

                        String[] parts = itemName.split(" ");

                        String itemNumStr = parts[0];

                        TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
                        EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
                        EditText et2 = (EditText) findViewById(R.id.etQtyHave);

                        tvLineName.setText(itemName);


                        String b4Space = itemNumStr.replaceAll("\\D.\\D+", "");
                        tv2.setText(b4Space);
                        et1.setText(b4Space);
                        et2.setText(b4Space);

                        calculateKeyIngredientPercent();
                    } catch (Exception e) {

                        Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
                    }

                }

            });
    android.app.AlertDialog alert = builder.create();
    alert.show();

// Toast.makeText(SelectConvertMethod.this, sb, Toast.LENGTH_LONG).show();

}

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多