向TextView显示值

时间:2013-11-07 08:44:57

标签: android

我在向TextView显示值时遇到问题。 例如,我将输入
1,2,3,4然后我想在我的TextView中以这种方式显示输出..我怎么能这样做?请帮帮我,提前谢谢你 1次出现1次 2次出现1次 3次出现1次 4次出现1次 这是我的代码:

String []values = ( sum.getText().toString().split(","));
double[]  convertedValues = new double[values.length];

    Arrays.sort(convertedValues);
    int i=0;
    int c=0;
            while(i<values.length-1){
            while(values[i]==values[i+1]){
            c++; 
                i++;  
             }   

            table.setText(values[i] + " appeared " + c + " times");          
        c=1;
        i++;
            if(i==values.length-1)
            table.setText(values[i] + " appeared " + c + " times");  

2 个答案:

答案 0 :(得分:1)

让你的textView支持multipleLines,然后在代码中创建一个StringBuffer并将结果附加到它,如

resultString.append(result).append(" appeared").append(c).append(" times\n");

之后你为textView设置文字,如:

textView.setText(resultString.toString());

答案 1 :(得分:1)

这是一个想法:

    // this is test string, you can read it from your textView
    String []values = ( "2, 1, 3, 5, 1, 2".toString().split(","));
    int [] intValues = new int[values.length];
    // convert string values to int
    for (int i = 0; i < values.length; ++i) {
        intValues[i] = Integer.parseInt(values[i].trim());
    }

    // sort integer array
    Arrays.sort(intValues);

    StringBuilder output = new StringBuilder();
    // iterate and count occurrences
    int count = 1;
    // you don't need internal loop, one loop is enough
    for (int i = 0; i < intValues.length; ++i) {
        if (i == intValues.length - 1 || intValues[i] != intValues[i + 1]) {
            // we found end of "equal" sequence
            output.append(intValues[i] + " appeared " + count + " times\n");
            count = 1; // reset count
        } else {
            count++; // continue till we count all equal values
        }
    }

    System.out.println(output.toString()); // prints what you extected
    table.setText(output.toString()); // display output