Android 按id查找textview时遇到问题
我使用一个菜单充气器,在每一行我给textview和seekbar一个I的id(我使用for循环给它们一个id)。当我得到seekbar的id并且我想在textview中插入文本时。我遇到的问题是如何在textview中设置与seekbar值相同的文本Android 按id查找textview时遇到问题,android,textview,layout-inflater,Android,Textview,Layout Inflater,我使用一个菜单充气器,在每一行我给textview和seekbar一个I的id(我使用for循环给它们一个id)。当我得到seekbar的id并且我想在textview中插入文本时。我遇到的问题是如何在textview中设置与seekbar值相同的文本 public static void setNewTipAmount(SeekBar barChanged, double amountForTip) { int getID = barChanged.getId(); //get seekb
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);
}
for(int i=0;i
我建议在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);
}
您能否发布代码,说明如何分配ID并将其添加到布局中。除非确定视图层次结构,否则应保持ID不同。我可以看看你的观点,给你一个可能的解决方案
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");
}