0

2番目の膨張したビューからユーザー入力を取得できないというこの問題があります。 SOに直接画像を投稿することはまだ許可されていないため、このサイトhttp://postimage.org/image/b4syhdzrr/に画像をアップロードしました。

ご覧のとおり、TextView は上位 2 つの EditText 入力 + 計算のみを表示しますが、3 番目の入力は考慮されませんでした (数値の最初の EditText は膨張しません)。

エンド ユーザーが必要とする EditText の数がわからないためです。すべての EditText からユーザー入力を取得するにはどうすればよいですか?

私が試したのは、ユーザー入力を OnClickListener 内の TextWatcher 内に格納する SharePreferences を設定することですが、このコードでは、TextWatcher が空であっても、Plus ボタンの OnClickListener によってアプリがクラッシュします。

public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            int position = spinner.getSelectedItemPosition();
            switch (position) {
            case 0:
                ll = (LinearLayout) findViewById(R.id.llSetView);
                ll.removeAllViews();
                View person1 = View.inflate(BillCalculator1.this,R.layout.person1, null);
                ll.addView(person1);

                btP1Add = (Button) ll.findViewById(R.id.buttonP1Add);
                btP1Gst = (Button) ll.findViewById(R.id.buttonP1GST);
                btP1Add.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        llNewRow = (LinearLayout) findViewById(R.id.llP1AddNewRow);
                        etNewRow = (EditText) findViewById(R.id.editTextNewRow);
                        View newRow = View.inflate(BillCalculator1.this,R.layout.newrow, null);
                        llNewRow.addView(newRow);

                        TextWatcher input = new TextWatcher() {
                            public void afterTextChanged(Editable s) {
                            }
                            //SharedPreferences here

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

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

                    }
                });

膨張したビューの XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editTextNewRow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal"
    android:hint="Enter new amount">

    <requestFocus />
</EditText>

プログラミングと Android の初心者です。追加情報が必要な場合はお知らせください。どうもありがとうございました。

4

1 に答える 1

0

私はあなたがやろうとしていることに別のアプローチを試みます。TextWatcherインフレートにスペシャルを追加しEditText、ユーザーが入力した値をに格納しますArrayList。まず、クラスに2つの追加フィールドが必要です。

private ArrayList<String> mData = new ArrayList<String>(); // this will store the entered values
private static int counter = 0; // to identify the rows

TextWatcher次のようなインターフェイスを実装するクラスを作成します。

    public class InputWatcher implements TextWatcher {

            private int mRowNumber;

            public InputWatcher(int rowNumber) {
                mRowNumber = rowNumber;
            }

            @Override
            public void afterTextChanged(Editable s) {
                // here you'll add the text as the user enters it in the correct position in the
                mData.set(mRowNumber, s.toString());                    
            }
     }

それで:

int position = spinner.getSelectedItemPosition();
switch (position) {
case 0:
     mData.clear();
     counter = 0;
     ll = (LinearLayout) findViewById(R.id.llSetView);
     ll.removeAllViews();
     // I sure hope that you have the Buttons with the id button1Add and buttonP1GST in the layout that you inflate
     // otherwise the code will throw a NullPointerException when you use them below
     View person1 = View.inflate(BillCalculator1.this,R.layout.person1, null);
     ll.addView(person1);
     btP1Add = (Button) ll.findViewById(R.id.buttonP1Add);
     btP1Gst = (Button) ll.findViewById(R.id.buttonP1GST);
     btP1Add.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
                 // add an entry in the data holder for this row
        mData.add("");
        View newRow = View.inflate(BillCalculator1.this,
                        R.layout.newrow, null);
        EditText justAdded = (EditText) newRow
                        .findViewById(R.id.editTextNewRow);
        justAdded.addTextChangedListener(new InputWatcher(counter));
        ll.addView(newRow);
        counter++;
           }
     });

合計を計算するときは、次ButtonOnClickListenerようにします。

@Override
public void onClick(View v) {
        int storedSize = mData.size();
    int sum = 0;
    for (int i = 0; i < storedSize; i++) {
        sum += Integer.parseInt(mData.get(i).equals("") ? "0"
                            : mData.get(i));
    }
    Toast.makeText(getApplicationContext(), "Total" + sum,              Toast.LENGTH_SHORT).show();
}

EditTextあなたの写真から、あなたがすでにレイアウトの1つ(+の左側にあるもの)から始めているかどうかはわかりませんでしたButton。この場合、行を追加する場所を追加する前に、を手動で設定し、インクリメントInputWatcherを移動する必要があります。counterInputWatcher

于 2012-06-30T10:51:35.170 に答える