2017-02-09 6 views
2

しばらくの間質問されていないので、それはずっと延滞しています!Androidの複数のレイアウトからEditTextの値を取得できない?

私はジョブアイテムを作成することができるアプリケーションを作成しています(新規の行には説明(EditText)、価格(EditText)、現在の行を削除するボタンが含まれていますが、複数の行がある場合は、EditTextフィールドの値を返します。最新の行の値を返します。

「ジョブリストコンテナ」とは別に、ビューは動的に作成されるため、XMLの不足は許されますが、私が達成しようとしている構造は次のとおりです。複数の行も)、送信ボタンをクリックすると、(価格を追加し、DBにジョブを追加)説明と価格の値のすべてを受け取り、それらを処理します。

enter image description here

...、これはコードです私はaddNewJobRow onClickリスナーから呼び出されたために書いています(簡略化のためにまとめて):

private void addJobItem() { 

    //Create a new row container 
    final LinearLayout jobRowContainer = new LinearLayout(this); 

    //Create a new EditText for the Description 
    final EditText description = new EditText(this); 
    description.setHint("Description..."); 
    description.setLayoutParams(new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      1.0f 
    )); 

    //Create an EditText for the Price 
    final EditText price = new EditText(this); 
    price.setHint("00.00"); 

    //Create a new button to delete the row 
    Button delete = new Button(this); 
    delete.setBackgroundColor(Color.RED); 
    delete.setText("X"); 

    //Add Description, Price and Delete to the row container 
    jobRowContainer.addView(description); 
    jobRowContainer.addView(price); 
    jobRowContainer.addView(delete); 

    //Add the Row Container to the Jobs List Container 
    ll_jobListContainer.addView(jobRowContainer); 

    //Get the values of the Description and Price, for each row 
    btn_JobSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      for (int i = 0; i < ll_jobListContainer.getChildCount(); i++) { 
       for(int j = 0; j < jobRowContainer.getChildCount(); j++) { 

        if (jobRowContainer.getChildAt(i) instanceof EditText){ 
         String descriptionString = description.getText().toString(); 
         String priceString = price.getText().toString(); 

         System.out.println("z! " + descriptionString + " @ " + priceString); 
        } 
       } 
      } 
     } 
    }); 
} 

instanceofを使用して、または使用しないで、FORループをネストした場合としない場合の2回の繰り返しを試しましたが、すべて最新の行が出力されます。

私は複数のジョブ行がある場合、どのように必要なすべての値を取得できますか?

お時間をありがとうとすべてが素敵なスタッフxxxの原因のうち

答えて

2

基本的な問題は、各行インスタンスの代わりにdescriptionpriceの最後のインスタンスのみを使用していることです。 (これはドミトリーが言っているものかもしれない)。これを修正するには、各行の入力を取得する必要があります。ここには1つの方法があります。

IDをdescription & priceに設定します。 (「1」または「2」を使用することはできません。リソースタイプIDである必要があるため、一意であることが保証されています)。私は行&のダミーのレイアウトファイルを2 EditTextに割り当てました。それを行うより良い方法があるかもしれません。とにかく、

descripton.setId(R.id.description);price.setId(R.id.price);

は今、これはあなたのonClick()で、あなたの宣言でこれらの2行を追加し

public void onClick(View v) { 
    for (int i = 0; i < ll_jobListContainer.getChildCount(); i++) { 
     LinearLayout currentRow = (LinearLayout)ll_jobListContainer.getChildAt(i); 
     EditText editText = (EditText)currentRow.findViewById(R.id.description); 
     String descriptionString = editText.getText().toString(); 

     editText = (EditText)currentRow.findViewById(R.id.price); 
     String priceString = editText.getText().toString(); 
     Log.d(TAG, "z! " + descriptionString + " @ " + priceString); 
    } 
} 

EDIT:は、私はそれ以来、この答えを変更したくありませんでしたすでに受け入れられていたので、私は別の答えでより簡潔な解答をしました。

+0

Garry99さん、ありがとうございました。乾杯! –

+0

助けてくれてうれしいです。私はちょうど私がある場所に '記述'を、別の場所に 'desc'を持っていることに気づいた。修正しました。申し訳ありません。 – Gary99

+0

拡張回答ありがとうございます。私は説明と価格のためにresourceIDを割り当てることができるレイアウトが必要なので、これに似た何かをしましたが、ダミーアイテムとして残しました。私の無知を許してください。しかし、最初の方法がうまくいくので、この追加は何を達成しますか? –

1

、あなたの最後のsetOnClickListenerは、文字列に

文字列をdescriptionString = description.getText()のtoString()かかり。

String priceString = price.getText()。toString();

説明と価格は、関数内のフィールドです(最後の編集テキスト)。 これを行う良い方法は、ViewHolderの "onTextChangeListner"でRecyclerView/ListViewを使用して、このオブジェクトのモデルに新しいテキストを保存し、ビューから直接ではなくモデルからすべてのテキストを出力することです。

+0

おかげで、しかし、脇リストビューから、彼らは私のための2つの新しいものです。私はViewHolderのためのドキュメントを見てきました、それはより多くのその行の情報を保持できますか?それについて精巧な要求をする機会がありますか? –

+0

ViewHolderは任意のレイアウトを保持できます。アイテムを10行のLinearLayoutとして作成することができます。 1)繰り返しアイテムがある場合は、ListView/RecyclerViewに入れて管理してください。 2)アイテムにデータが含まれている場合は、そのデータのモデルをさまざまな理由 - 要素がより多く画面を保持できるようになった場合にデータを失わず、アクティビティが再作成されたときに状態を適切に復元すること。 – DmitryBorodin

0

私は通常、必要ではないコードを変更するのではなく、尋ねられた質問にのみ答えようとします。しかし、この場合、リソースIDを取得するためだけにダミーレイアウトを作成していたので、そのレイアウトファイルを使用できるかどうか疑問に思っています。私は自分の答えを変え始めましたが、私が変更を加える前に元のものが受け入れられました。私はここにソリューションの別のバージョンを入れました。私はすでに受け入れられている回答を変更したくなかった。返信用

private void addJobItem() { 
    //Create a new row container 
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout jobRowContainer = (LinearLayout)inflater.inflate(R.layout.row_layout, null); 

    //Add the Row Container to the Jobs List Container 
    ll_jobListContainer.addView(jobRowContainer); 

    //Get the values of the Description and Price, for each row 
    btn_JobSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      for (int i = 0; i < ll_jobListContainer.getChildCount(); i++) { 
       LinearLayout currentRow = (LinearLayout)ll_jobListContainer.getChildAt(i); 
       EditText editText = (EditText)currentRow.findViewById(R.id.description); 
       String descriptionString = editText.getText().toString(); 

       editText = (EditText)currentRow.findViewById(R.id.price); 
       String priceString = editText.getText().toString(); 
       Log.d(TAG, "z! " + descriptionString + " @ " + priceString); 
      } 
     } 
    }); 
} 

row_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/single_row"> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="Description..." 
     android:id="@+id/description" 
     /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="00.00" 
     android:id="@+id/price" 
     /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_dark" 
     android:text="X" 
     android:id="@+id/clear_button" 
     /> 
</LinearLayout> 
関連する問題