문제

I use a menu inflater and in every row I give the textview and seekbar an id of i(I use a for loop to give them there id). When I get the id of the seekbar and I want to insert text into the textview. The problem I am having is how do I set the text in the textview that has the same value as the seekbar.

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");

}

}

Here is the code to how I assign the Id's

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

    }
도움이 되었습니까?

해결책

I suggest creating a simple mapping between TextView id and Seekbar id. This way each id will be different and you can still get the corresponding item id. Here is my solution:

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

Now to find the textview:

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");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top