In my app the user chooses how many seekbars they need and then my code generates them (see below).
// dynamically creates the view objects
tL = (TableLayout)findViewById(R.id.tableLayout1);
// creates all the fields
for(int i = 1; i <= numOfStockTanks; i++) {
TableRow tR = new TableRow(this);
// creates the textView
tV1 = new TextView(this);
tV1.setText(" " + tankNum[i - 1] + ": ");
tV1.setPadding(2, 0, 0, 0);
// creates the seekBar
sBGauge = new SeekBar(this);
sBGauge.setMax(depthL - 1);
sBGauge.setMinimumWidth(150);
sBGauge.setId(2000 + 1);
sBGauge.setOnSeekBarChangeListener(this);
// shows the progress of the seekBar
tVGauge = new TextView(this);
tVGauge.setText("0-0.0\"");
tVGauge.setId(3000 + i);
// adds objects to row
tR.addView(tV1);
tR.addView(sBGauge);
tR.addView(tVGauge);
// add the TableRow to the TableLayout
tL.addView(tR, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// creates new tableRow
TableRow tR2 = new TableRow(this);
// add's a "+" button to tableRow
buttonPlus = new Button (this);
buttonPlus.setWidth(60);
buttonPlus.setText("+");
buttonPlus.setId(4000 + i);
buttonPlus.setOnClickListener(new MyPOnClickListener());
// add's a "-" button to tableRow
buttonMinus = new Button (this);
buttonMinus.setWidth(60);
buttonMinus.setText("-");
buttonMinus.setId(5000 + i);
buttonMinus.setOnClickListener(new MyMOnClickListener());
// add TextView tVChange to show change from previous sqlite entry
tVChange = new TextView (this);
tVChange.setText("Change");
tVChange.setId(6000 + i);
// add the TextView and the buttons to the new TableRow
tR2.addView(buttonPlus);
tR2.addView(buttonMinus);
tR2.addView(tVChange);
// add the TableRow to the TableLayout
tL.addView(tR2,new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} // end for statement
my problem is when I use my onProgressChanged method:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
int seekId = sBGauge.getId();
seekId = seekId - 2000;
try {
tVGauge = (TextView) findViewById(seekId + 3000);
} // end try
catch (Exception e) {Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();}
try {
// sets the text of the textview
tVGauge.setText(depth.get(progress));
} // end try
catch (Exception e) {}
manualP = progress;
}
My problem is that all my seekBars only change the first progress bars textView, the buttons change the proper text view.
Thanks for your help
You are setting the same id to each seekbar
sBGauge.setId(2000 + 1);
change 1 to i
sBGauge.setId(2000 + i);