2013-10-23 89 views
0

我使用菜單inflater,並在每一行中給textview和seekbar一個id我(我使用for循環給他們在那裏id)。當我得到seekbar的id,並且我想在textview中插入文本。我遇到的問題是如何在textview中設置與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); 

    } 
+0

你可以發佈您展示如何分配他們的ID,並將它們添加到佈局的代碼。除非您確定視圖層次結構,否則應該保持id不同。可以看看你的觀點並給你一個可能的解決方案 –

回答

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