2012-07-11 47 views
2

我不是一名程序員,我只是想讓應用程序幫助我完成工作,所以我不知道編程的正確性。這就是說,這是我所反對的。我輸入一個數字1到100,然後我的應用程序將創建一個可滾動的tablelayout與多行。每一行都有一個TextView,一個EditText和其他的TextView這裏我的代碼:當edittext在動態創建的tablelay中更改1到50行的任何位置時,將更改textview?

TableLayout tL = (TableLayout)findViewById(R.id.tableLayout1); 
    // creates all the fields 
    for(int i = 1; i <= numOfInjWells; i++) { 
     TableRow tR = new TableRow(this); 
     // creates the textView 
     TextView tV1 = new TextView(this); 
     tV1.setText("  " + i + ": "); 

     // add edit text 
     EditText eT = new EditText(this); 
     eT.setText("Meter Reading"); 
     eT.setInputType(InputType.TYPE_CLASS_NUMBER); 

     TextView tV2 = new TextView(this); 
     tV2.setText(""); 

     // add the TextView and the editText to the new TableRow 
     tR.addView(tV1); 
     tR.addView(eT); 
     tR.addView(tV2); 

     // add the TableRow to the TableLayout 
     tL.addView(tR,new TableLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
    } // end for statement 

所以當我輸入一個號碼在EditText上(ET)一排的,我希望它然後計算它與數之間的差異我已經存儲在我的數據庫中(我已經知道如何從數據庫中獲得我需要的特定數字),然後在第一行中更改textview tV2,以便不顯示按鈕即可顯示差異。我遇到的問題是如何將第一行的edittext(eT)與textview(tV2)相關聯,因爲所有edittext和textviews都具有相同的名稱eT或tV2

感謝您的幫助,我很抱歉我對編碼知之甚少。

編輯:我想過加入

eT.setId(i); 
tV2.setId(i); 

,但我不知道如何使用,在我的計算。

回答

0

在查看listview之後,我無法讓他們使用edittext,所以我回到了我的tablelayout,這是最終的工作版本。

在這裏,我創建領域,加入的EditText一個textwatcher要監視更改:

TableLayout tL = (TableLayout)findViewById(R.id.tableLayout1); 
    // creates all the fields 
    for(int i = 1; i <= numOfInjWells; i++) { 
     TableRow tR = new TableRow(this); 
     // creates the textView 
     TextView tV1 = new TextView(this); 
     tV1.setText("  " + i + ": "); 

     // add edit text 
     eT = new EditText(this); 
     eT.setInputType(InputType.TYPE_CLASS_NUMBER); 
     eT.setWidth(100); 
     eT.setId(1000 + i); 
     eT.addTextChangedListener(new CustomTextWatcher(eT)); 

     tV2 = new TextView(this); 
     tV2.setText(""); 
     tV2.setId(2000 + i); 

     // add the TextView and the editText to the new TableRow 
     tR.addView(tV1); 
     tR.addView(eT); 
     tR.addView(tV2); 

     // add the TableRow to the TableLayout 
     tL.addView(tR,new TableLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
    } // end for statement 

下一個我創造了這個類我customtextwachter:

private class CustomTextWatcher implements TextWatcher { 
    private EditText mEditText; 

    public CustomTextWatcher(EditText eT) { 
     mEditText = eT; 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    } 

    public void afterTextChanged(Editable s) { 
     // gets the change amount for each meter reading from the previous reading 
      // this method does the work 
     getMeterChange(); 

    } 
} // end class CustomTextWatcher 

的getMeterChange()方法工作:

public void getMeterChange() { 
    int preMeterReading = 0; 
    int mReading = 0; 

    try { 
     cRWLogData.moveToPosition(recordLookUp - 1); 
     preMeterReading = cRWLogData.getInt(14 + rowChanged); 
     // finds the edittext that has focus 
     View currView = tL.findFocus(); 
     int currentid = tL.findFocus().getId(); 
     // gets the string from the edittext and changes it to a int 
     EditText currentComponent = (EditText) currView; 
     String eTValue = currentComponent.getText().toString(); 
     mReading = Integer.parseInt(eTValue); 
     // calculates difference for what is entered and what is in database 
     mChange = mReading - preMeterReading; 
     // makes the textview in the same tablerow as the edittext active 
     TextView tV2 = (TextView) findViewById(currentid + 1000); 
     // sets the text of the textview 
     tV2.setText("  " + mChange +""); 
    } // end try 
    catch (Exception e) {} 

} // end getMeterChange 
0

的問題,我來跨越是怎麼做的我在一排關聯的EditText (ET)用的TextView(TV2),因爲所有的EditText和 textviews具有相同的名稱等或TV2

一個簡單的選項是爲這兩個Views設置ID,它們之間有一個關係,當某個特定的EditText被修改時,您會發現哪個TextView更靠近EditText更新它。例如,要設置的ID:

// add edit text 
EditText eT = new EditText(this); 
et.setId(1000 + i); 
eT.setText("Meter Reading"); 
eT.setInputType(InputType.TYPE_CLASS_NUMBER); 

TextView tV2 = new TextView(this); 
tV2.setId(2000 + i); // as you see the difference between the this id and the EditText is 1000 
tV2.setText(""); 

因此,如果您要爲您要計算值你可以簡單的EditText參考做findViewByIdEditText的ID向其中添加1000。現在,我不知道如何計算基於EditText的值,如果您使用TextWatcher,則可以創建自己的類(實現TextWatcher界面),該界面也需要int代表您爲其創建的EditText的ID設置觀察者並從中找到想要的TextView

另外請注意,在每行3 Views(+ 1 TableRow),你可以在應用程序的內存惹上麻煩,如果你創建50行或更多(因爲這將導致你的佈局約200 Views)。值得看看ListView

+0

我已經查看了列表視圖,但由於缺乏編碼知識,我迷上了他們,或者我不能讓他們編譯沒有錯誤。我一直在試圖找到教程來幫助我,但我沒有提出足夠接近我正在做的事情,以便能夠滿足我的需求。感謝您的幫助,我會嘗試這個,看看我能不能做到這一點 – deerkiller11 2012-07-11 20:54:27

+0

@ deerkiller11你設法做些什麼?另外我對ListView的建議可能不是一個好主意,因爲ListView行中的EditText不能很好地工作。另一種方法是保持當前的表格佈局並構建某種分頁以僅顯示錶格的一部分。 – Luksprog 2012-07-13 09:42:59

+0

不是我正在查看listview選項,但我還沒有能夠得到它的工作。 – deerkiller11 2012-07-15 03:37:58

0

我知道這是舊的,但對於那些誰發現這一點時,清我會更新一個更好的選擇......

TableLayout tL = (TableLayout)findViewById(R.id.tableLayout1); 
// creates all the fields 
for(int i = 1; i <= numOfInjWells; i++) { 
    TableRow tR = new TableRow(this); 
    // creates the textView 
    TextView tV1 = new TextView(this); 
    tV1.setText("  " + i + ": "); 

    // add edit text 
    eT = new EditText(this); 
    eT.setInputType(InputType.TYPE_CLASS_NUMBER); 
    eT.setWidth(100); 


    tV2 = new TextView(this); 
    tV2.setText(""); 
    eT.addTextChangedListener(new CustomTextWatcher(tV2)); 

    // add the TextView and the editText to the new TableRow 
    tR.addView(tV1); 
    tR.addView(eT); 
    tR.addView(tV2); 

    // add the TableRow to the TableLayout 
    tL.addView(tR,new TableLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
} // end for statement 

然後觀察者:

private class CustomTextWatcher implements TextWatcher { 
    private TextView mTextView; 

    public CustomTextWatcher(TextView tV2) { 
     mTextView = tV2; 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

    public void onTextChanged(CharSequence s, int start, int before, int count) {} 

    public void afterTextChanged(Editable s) { 
     // gets the change amount for each meter reading from the previous reading 
      // this method does the work 

     cRWLogData.moveToPosition(recordLookUp - 1); 
     preMeterReading = cRWLogData.getInt(14 + rowChanged); 
     // edit text has just passed the value in Editable s 
     mReading = Integer.parseInt(s.toString()); 
     // calculates difference for what is entered and what is in database 
     mChange = mReading - preMeterReading; 
     // sets the text of the textview 
     mTextView.setText("  " + mChange +""); 

    } 
} // end class CustomTextWatcher 

還有的值傳遞到觀察者的一個原因。使用框架爲你做硬球場。

注意:我沒有真正編譯過,而且您可能需要將try/catch放回去,爲了清楚起見,我只是將其刪除。