从动态生成的搜索栏中获取id / tag

时间:2012-09-02 13:43:24

标签: android seekbar

在我的应用程序中,用户选择他们需要多少个搜索栏,然后我的代码生成它们(见下文)。

    // 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

我的问题是当我使用onProgressChanged方法时:

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

我的问题是我的所有seekBars只更改了第一个进度条textView,按钮改变了正确的文本视图。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

您为每个搜索栏设置相同的ID

   sBGauge.setId(2000 + 1);

将1更改为 i

   sBGauge.setId(2000 + i);