0

我使用了一个菜单充气器,并且在每一行中,我都给 textview 和 seekbar 一个 id 的 i(我使用一个 for 循环来给他们 id)。当我得到 seekbar 的 id 并且我想将文本插入到 textview 中时。我遇到的问题是如何在文本视图中设置与搜索栏具有相同值的文本。

public static void setNewTipAmount(SeekBar barChanged, double amountForTip) {

 int getID = barChanged.getId(); //get seekbar id


    TextView textView = tipPerPerson.findViewById(getID);// get the textview with same id
            textView.setText("Hello");

}

}

这是我如何分配 ID 的代码

        for (int i = 0; i < InBetweenUIAndBusinessLogic.getGuests(); i++) {
        View v = in.inflate(R.layout.row_per_person, table, false);
        double tipPerIndividual = (Math.round(InBetweenUIAndBusinessLogic
                .getTipPerPerson() * 100.0) / 100.0);
        String tip = Double.toString(tipPerIndividual);
        tipPerPerson = (TextView) v.findViewById(R.id.tipPerPerson);
        tipPerPerson.setId(i);
        tipPerPerson.setText(tip);
        rows.add(tipPerPerson);
        percentageTip = (SeekBar) v.findViewById(R.id.seekBar1);
        percentageTip.setOnSeekBarChangeListener(new myListener());
        double guests = InBetweenUIAndBusinessLogic.getGuests();
        percentageTip.setProgress((int) (100 / guests));
        percentageTip.setId(i);
        table.addView(v);
        bars.add(percentageTip);

    }
4

1 回答 1

1

TextView我建议在id 和id之间创建一个简单的映射Seekbar。这样每个 id 都会不同,您仍然可以获得相应的 item id。这是我的解决方案:

mGuestCount = InBetweenUIAndBusinessLogic.getGuests();

for (int i = 0; i < InBetweenUIAndBusinessLogic.getGuests(); i++) {
    View v = in.inflate(R.layout.row_per_person, table, false);
    double tipPerIndividual = (Math.round(InBetweenUIAndBusinessLogic
        .getTipPerPerson() * 100.0) / 100.0);
    String tip = Double.toString(tipPerIndividual);
    tipPerPerson = (TextView) v.findViewById(R.id.tipPerPerson);
    tipPerPerson.setId(i);
    tipPerPerson.setText(tip);
    rows.add(tipPerPerson);
    percentageTip = (SeekBar) v.findViewById(R.id.seekBar1);
    percentageTip.setOnSeekBarChangeListener(new myListener());
    double guests = InBetweenUIAndBusinessLogic.getGuests();
    percentageTip.setProgress((int) (100 / guests));
    percentageTip.setId(i + mGuestCount);
    table.addView(v);
    bars.add(percentageTip);
}

现在找到文本视图:

public static void setNewTipAmount(SeekBar barChanged, double amountForTip) {
    int getID = barChanged.getId(); //get seekbar id

    TextView textView = tipPerPerson.findViewById(getID - mGuestCount);// get the textview with corresponding id
    textView.setText("Hello");
}
于 2013-10-23T02:41:42.867 回答