2

私はプログラマーではないので、自分の仕事に役立つアプリを作るのが好きなので、適切なプログラミングについてはよくわかりません。そうは言っても、これは私が反対していることです。1から100までの数値を入力すると、アプリはその数の行を含むスクロール可能なテーブルレイアウトを作成します。各行には、textview、edittext、および別の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

したがって、1行目のエディットテキスト(eT)に数値を入力するときに、その数値とデータベースに保存した数値との差を計算します(データベースから必要な特定の数値を取得する方法はすでに知っています)。次に、1行目のtextview tV2を変更して、ボタンをクリックしなくても違いが表示されるようにします。私が遭遇している問題は、すべてのエディットテキストとテキストビューが同じ名前eTまたはtV2を持っているため、1行目のエディットテキスト(eT)をテキストビュー(tV2)に関連付ける方法です。

ご協力いただきありがとうございます。コーディングについてよく知らないことをお詫び申し上げます。

編集:私は追加することを考えました

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

しかし、それを計算に使用する方法がわかりません。

4

3 に答える 3

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 を元に戻す必要がある可能性があります。わかりやすくするために削除しました。

于 2013-03-06T12:35:41.367 に答える
0

リストビューを調べた後、編集テキストで動作させることができなかったので、テーブルレイアウトに戻りました。これが最終的な作業バージョンです。

ここでは、フィールドを作成し、変更を監視するために edittext にテキストウォッチャーを追加します。

    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
于 2012-07-16T20:40:49.713 に答える