-1

嗨,有人可以建议我一个示例,说明如何根据 textviews 中的数字对 textviews 进行排序。我能够从需要排序的 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;

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

    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);
    }
}
4

2 回答 2

2

要对您的 textViews 进行排序,首先将它们放入一个数组中,

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

请注意,如果您有父容器的句柄,则可以使用ViewGroup.getChildCount()and轻松构建数组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());
于 2012-07-31T15:49:55.743 回答
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]);
}
于 2012-07-31T15:42:46.577 回答