我使用菜单inflater,在每一行中我给textview和seekbar一个id的id(我使用for循环给它们id)。当我得到搜索栏的id并且我想在textview中插入文本。我遇到的问题是如何在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);
}
答案 0 :(得分:1)
我建议在TextView
id和Seekbar
id之间创建一个简单的映射。这样每个ID都会有所不同,您仍然可以获得相应的项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);
}
现在找到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");
}