如何在TextViews中对数字进行排序并显示它们?

时间:2012-07-31 15:28:45

标签: android sorting textview

嗨,有人可以建议我一个示例,说明我如何根据textview中的数字对文本视图进行排序。我能够从TextViews中获取文本需要先排序并放置最小的数字。

谢谢。

public void sortNumbers(View v) {

    String[] numbers = new String[7];
    numbers[0] = textView23.getText().toString();
    numbers[1] = textView33.getText().toString();
    numbers[2] = textView43.getText().toString();
    numbers[3] = textView53.getText().toString();
    numbers[4] = textView63.getText().toString();
    numbers[5] = textView73.getText().toString();
    numbers[6] = textView83.getText().toString();

    Integer[] intValues = new Integer[numbers.length];
    for (int i = 0; i < numbers.length; i++) {
        intValues[i] = Integer.parseInt(numbers[i].trim());
    }
    Collections.sort(Arrays.asList(intValues));

    for (int i = 0; i < intValues.length; i++) {
        Integer intValue = intValues[i];

        //here I want to assign sorted numberes to the TextViews


    }

}

所以我遵循了杰弗里的建议。这是仍然无法正常工作的代码。可能有什么不对?

创建了一个TextViews数组:

TextView[] tvs = new TextView[7];

    tvs[0] = textView23;
    tvs[1] = textView33;
    tvs[2] = textView43;
    tvs[3] = textView53;
    tvs[4] = textView63;
    tvs[5] = textView73;
    tvs[6] = textView83;

对数组进行排序并将新值分配给TextViews:

    Arrays.sort(tvs, new TVTextComparator());

    textView23.setText(tvs[0].getText().toString());
    textView33.setText(tvs[1].getText().toString());
    textView43.setText(tvs[2].getText().toString());
    textView53.setText(tvs[3].getText().toString());
    textView63.setText(tvs[4].getText().toString());
    textView73.setText(tvs[5].getText().toString());
    textView83.setText(tvs[6].getText().toString());    

这是Comporator类:

public class TVTextComparator implements Comparator<TextView> {
    public int compare(TextView lhs, TextView rhs) {

        Integer oneInt = Integer.parseInt(lhs.getText().toString());
        Integer twoInt = Integer.parseInt(rhs.getText().toString());

        return oneInt.compareTo(twoInt);
    }
}

2 个答案:

答案 0 :(得分:2)

对textViews进行排序,首先将它们放在一个数组中,

TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
// and so on

请注意,如果您拥有父容器的句柄,则可以使用ViewGroup.getChildCount()getChildAt()轻松构建阵列。

现在为文本视图编写一个比较器,

class TVTextComparator implements Comparator<TextView> {
  @Override
  public int compare(TextView lhs, TextView rhs) {
    return lhs.getText().toString().compareTo(rhs.getText().toString());
    // should check for nulls here, this is NOT a robust impl of compare()
  }
}

现在使用比较器对数组进行排序,

Arrays.sort(tvs, 0, tvs.length, new TVTextComparator());

答案 1 :(得分:0)

public void sortNumbers(View v) {

String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();

Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
    intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));

  textView23.setText(intValues[0]);
  textView33.setText(intValues[1]);
  textView43.setText(intValues[2]);
  textView53.setText(intValues[3]);
  textView63.setText(intValues[4]);
  textView73.setText(intValues[5]);
  textView83.setText(intValues[6]);
}