1

私のアンドロイドアプリでTableLayoutに新しいテーブルの行を追加しようとしています。アンドロイドチュートリアルに従うことで、複数のエディットテキストを含む新しいテーブル行を追加できました。私がしようとしているのは、TextWatcherを使ってそれらのエディットテキストのユーザー値を取得する必要があるということです。私は多くのチュートリアルを検索して多くを試みましたが、失敗しました。 これは、これにボタンを追加する前の私のアプリの外観です。アンドロイドのテーブルレイアウトにダイナミックテーブルの行をテキストウォッチャーと一緒に追加

enter image description here

私は必要なもの、ユーザーが望んでいる場合は、ボタンをクリックすることにより、2列目を開くことであり、ユーザーはのEditTextフィールドに値を入力した場合、彼らは使用する必要があると、最終的な結果はそれに応じて表示されるはずです。 これは私の前のコードです。私は質問で投稿し、ここでそれを解決しました。 Using Android TextWatcher with multiple user input values and Update thses values

誰かが私を助けてくれる場合は、大変感謝しています。前もって感謝します。

は今、これは私の新しいXMLコード

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <Button 
     android:id="@+id/Button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:text="Add Line" /> 


    <ScrollView 
     android:id="@+id/scrollView2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/Button1" 
     android:layout_marginTop="97dp"> 


     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <TableLayout 
       android:id="@+id/TableLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:divider="?android:attr/dividerHorizontal" 
       android:showDividers="middle"> 

       <TableRow 
        android:id="@+id/TableRow1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 


        <EditText 
         android:id="@+id/editText1" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:ems="4" 
         android:padding="2dp" /> 

        <EditText 
         android:id="@+id/editText2" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:ems="4" 
         android:padding="2dp" /> 

        <EditText 
         android:id="@+id/editText3" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:ems="4" 
         android:padding="2dp" /> 

        <TextView 
         android:id="@+id/TextViewsub1" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:gravity="center" 
         android:hint="$ 0.00" 
         android:textSize="18dip" /> 


       </TableRow> 

       <TableRow 
        android:id="@+id/TableRow2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 


        <EditText 
         android:id="@+id/editText4" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:ems="4" 
         android:padding="2dp" /> 

        <EditText 
         android:id="@+id/editText5" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:ems="4" 
         android:padding="2dp" /> 

        <EditText 
         android:id="@+id/editText6" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:ems="4" 
         android:padding="2dp" /> 

        <TextView 
         android:id="@+id/TextViewsub2" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:gravity="center" 
         android:hint="$ 0.00" 
         android:textSize="18dip" /> 

       </TableRow> 
      </TableLayout> 
     </RelativeLayout> 
    </ScrollView> 

    <TextView 
     android:id="@+id/textView12" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/scrollView2" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginEnd="87dp" 
     android:layout_marginRight="87dp" 
     android:hint="Rs: 0.00" /> 

    <TextView 
     android:id="@+id/textView13" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/textView12" 
     android:layout_marginEnd="86dp" 
     android:layout_marginRight="86dp" 
     android:layout_toLeftOf="@+id/textView12" 
     android:layout_toStartOf="@+id/textView12" 
     android:text="Total Value" /> 

</RelativeLayout> 

でこれが今の私のJavaファイルは、以下のようにlinearlayot置く親レイアウトXML内のすべての

public class NewClass extends AppCompatActivity implements View.OnClickListener { 

     Button btnAdd; 
     int counter = 0; 

     EditText edit1, edit2, edit3; 
     TextView textViewSub1, textViewResult; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 

      /*First row variables*/ 
      edit1 = (EditText) findViewById(R.id.editText1); 
      edit2 = (EditText) findViewById(R.id.editText2); 
      edit3 = (EditText) findViewById(R.id.editText3); 
      textViewSub1 = (TextView) findViewById(R.id.TextViewsub1); 

      textViewResult = (TextView) findViewById(R.id.textView12); 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_new_class); 

      //add line button 
      btnAdd = (Button) findViewById(R.id.Button1); 
      btnAdd.setOnClickListener(this); 

      edit1.addTextChangedListener(new LashCustomTextWatcher1()); 
      edit2.addTextChangedListener(new LashCustomTextWatcher1()); 
      edit3.addTextChangedListener(new LashCustomTextWatcher1()); 

     } 

     public class LashCustomTextWatcher1 implements TextWatcher { 

      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
       textViewResult.setText(lashCalculate()); 
      } 
     } 


     public void onClick(View view) { 
      TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1); 
      TableRow tr = new TableRow(this); 

      //TextView tv = new TextView(this); 
      //tv.setText("text "); 

      edit1 = new EditText(this); 
      //edit1.setText("Hello " + counter++); 

      edit2 = new EditText(this); 
      //edit2.setText("Item no " + counter++); 

      edit3 = new EditText(this); 
      //edit3.setText("Qty no " + counter++); 

      textViewSub1 = new TextView(this); 


      tr.addView(edit1); 
      tr.addView(edit2); 
      tr.addView(edit3); 
      tr.addView(textViewSub1); 
      //tr.addView(cb); 
      tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT)); 
     } 


     public String lashCalculate() { 

      //declaring variables 
      double row1_value = 0; 

      DecimalFormat df = new DecimalFormat("0.00"); 

      //calculate first row 
      if (!edit1.getText().toString().equals("") && !edit2.getText().toString().equals("")) { 
       double num1 = Double.parseDouble((edit1.getText().toString())); 
       double num2 = Double.parseDouble((edit2.getText().toString())); 

       row1_value = num1 * num2; 

       double num3 = 0; 
       if (!edit3.getText().toString().equals("")) { 
        num3 = Double.parseDouble((edit3.getText().toString())); 
        row1_value = (((100 - num3) * num2) * num1)/100; 
       } 

       textViewSub1.setText(df.format(row1_value)); 
      } 


      return df.format(row1_value); 
    } 


    } 
+0

あなたは親ビューの動的追加ビューのaddView(ビュー)メソッドを使用することができます。 – ashish

+0

@ashish、私はedittextフィールドでaddView()を使用します。しかし、それは動作しません。私はアンドロイドの初心者であるので、あなたはそれを私に説明してもらえますか? – Lash

+0

私の答えを確認してください – ashish

答えて

0

1)まずどのように見えるかです:

<LinearLayout 
       android:id="@+id/parent_layout" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"></LinearLayout> 

2)Thanクリックごとに追加したいレイアウトを作成します。

3)あなたは今、以下のように親にあなたのビューを追加することができます。

LinearLayout parent_layout = (LinearLayout)findViewById(R.id.parent_layout); 
        for(i=0;i<3;i++) 
        { 
         parentlayout.addView(myViewReview(data.get(i))); 
        } 


public View myViewReview() { 

     View v; // Creating an instance for View Object 
     LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.row_facility_review, null); 
     TextView row_reviewer_name = (TextView) v.findViewById(R.id.row_reviewer_name); 
     TextView row_review_text = (TextView) v.findViewById(R.id.row_review_text); 
     return v; 
    } 
+0

あなたが私を助けてくれてありがとう、ありがとう、私があなたが言及したことを理解しようとするが、私は新しいので、問題が発生します。あなたが知っているなら、これを参照するチュートリアルや手順を教えてください。 – Lash

+0

それは私が答えであなたに提供している簡単なステップです。 – ashish

+0

私は試してみましょう、ありがとう、ありがとう – Lash

関連する問題