Android显示结果一个接一个

时间:2013-11-29 07:31:18

标签: java android

我开发了一个基于命理学的Android应用程序。每个字母都有这样的值.. A,J,S - 1 B,K,T - 2 C,L,U - 3 D,M,V - 4 E,N,W - 5 F,O,X - 6 G,P,Y - 7 H,Q,Z - 8 我,R - 9

当用户在此处输入他的名字时,应用程序将采用他的值...例如我的名字是'ROSHAN'然后值是R = 9,O = 6,S = 1,H = 8,A = 1,N = 5,我的应用程序将显示此名称中缺少的数字。所以结果将是[2,3,4,7] ..我需要一个接一个地展示...... 像

your first missing number : 2
your second missing number : 3
your third missing number : 4
your fourth missing number : 7

所以请帮忙。到目前为止,我正在给出我开发的代码..

MainActivity

public void gReport(View V) 
    {
        EditText et1 = (EditText) findViewById (R.id.editText1);
        EditText et2 = (EditText) findViewById (R.id.editText2);
        EditText et3 = (EditText) findViewById (R.id.editText3);
        TextView tv1 = (TextView) findViewById (R.id.textView1);
        List<Integer> sum1;
        sum1 = getMissingNo(et1.getText().toString() + et2.getText().toString());

        tv1.setText(String.valueOf(sum1));



    }

    private List<Integer> getMissingNo(String text) {
         ArrayList<Integer> missingNo = new ArrayList<Integer>();
         TextView tv1 = (TextView) findViewById (R.id.textView1);
            boolean[] usedNos = new boolean[9];
            for(int i=0; i<text.length(); i++){
                usedNos [(int) (value1(text.charAt(i))-1)] = true;
            }

            for(int i=0; i<9; i++){
                if(!usedNos[i]){
                    missingNo.add(i+1);
                    System.out.println((i+1) + " is missing");
                    tv1.setText(String.valueOf((i+1)));
                }
            }

            return missingNo;
        // TODO Auto-generated method stub

    }



    private long value1(char a) {
        // TODO Auto-generated method stub
        switch(a)
        {
           case 'A': 
           return 1;    
           case 'B':
           return 2;
           case 'C':
           return 3;
           case 'D':
           return 4;
           case 'E':
           return 5;
           case 'F':
           return 6;
           case 'G':
           return 7;
           case 'H':
           return 8;
           case 'I':
           return 9;
           case 'J':
           return 1;
           case 'K':
           return 2;
           case 'L':
           return 3;
           case 'M':
           return 4;
           case 'N':
           return 5;
           case 'O':
           return 6;
           case 'P':
           return 7;
           case 'Q':
           return 8;
           case 'R':
           return 9;
           case 'S':
           return 1;          
           case 'T':
           return 2;
           case 'U':
           return 3;
           case 'V':
           return 4;
           case 'W':
           return 5;
           case 'X':
           return 6;
           case 'Y':
           return 7;
           case 'Z':
           return 8;
           default:         
           return 0;

        }
    }

2 个答案:

答案 0 :(得分:1)

如果您可以显示第1,第2,第3 ......那么您可以试试这个:

public static String toOrdinal(int value) {
        int remainder = value % 10;
        switch (remainder) {
        case 1:
            return value + "st";
        case 2:
            return value + "nd";
        case 3:
            return value + "rd";
        default:
            return value + "th";
        }
    }

通过传递整数值来调用方法toOrdinal(number),返回的字符串将是第1,第2,第3,第4,第5 ...... ....

否则,如果您想要first, second, third,...,则可能是长行代码 - http://www.rgagnon.com/javadetails/java-0426.html

答案 1 :(得分:1)

如果要创建自定义数量的TextView(如果有更多缺少的数字,则需要更多TextView),您可以执行以下操作:

//Make a layout programmatically
LinearLayout lnLayout = new LinearLayout(this);
lnLayout.setLayoutParams(new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT));
lnLayout.setOrientation(LinearLayout.Horizontal);

//Your code was a bit unclear so I assume these
int[] missingNumber = new int[/*the ammount of missing numbers*/];
for (int iii < 0; iii < missingNumber.length; iii++)
    missingNumber[iii] = /* Here you should use something to get missing numbers one by one, a method like "GetNumber(iii)" or whatever suits your need :) */

//For each missing number create one text view (inside an array is better)
TextView[] text = new TextView[missingNumber.length];
for (int iii = 0; iii < missingNumber.length; iii++)
{
    text[iii] = new TextView(this);
    text[iii].setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
    text[iii].setText( String.valueOf( missingNum(iii) ) );
    lnLayout.addView(text[iii]);
}

setContentView(lnLayout);

另外,我建议进行此更改以使您的代码更清晰:

private long value1(char a) {
    switch(a)
    {
       case 'A': return 1;    
       case 'B': return 2;
       case 'C': return 3;
       case 'D': return 4;
       case 'E': return 5;
       case 'F': return 6;
       case 'G': return 7;
       case 'H': return 8;
       case 'I': return 9;
       case 'J': return 1;
       case 'K': return 2;
       case 'L': return 3;
       case 'M': return 4;
       case 'N': return 5;
       case 'O': return 6;
       case 'P': return 7;
       case 'Q': return 8;
       case 'R': return 9;
       case 'S': return 1;          
       case 'T': return 2;
       case 'U': return 3;
       case 'V': return 4;
       case 'W': return 5;
       case 'X': return 6;
       case 'Y': return 7;
       case 'Z': return 8;
       default:  return 0;

    }