2012-09-02 37 views
0

在我的應用程序中,用戶選擇他們需要多少個搜索欄,然後我的代碼生成它們(見下文)。從動態生成的搜索欄中獲取ID /標記

// 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,按鈕更改正確的文本視圖。

感謝您的幫助

回答

2

要設置相同的ID給每個搜索條

sBGauge.setId(2000 + 1); 

變化1至

sBGauge.setId(2000 + i); 
+0

謝謝,我不能相信我漏掉了這一點。我還在onProgressChanged方法中發現了另一個錯誤。我將第一行更改爲'int seekId = seekBar.getId();'我刪除了第二行(因爲它不是真的需要)並將第四行更改爲'tVGauge =(TextView)findViewById(seekId + 1000);'因爲我刪除了第二行 – deerkiller11

相關問題