2017-12-11 13 views
-3

私の要件は次のとおりです。+アイコンをクリックすると、新しい行が動的に追加され、そのIDにアクセスできるようになります。それを実装するための提案をしてください。動的な行アイテムをアクティビティに追加してそのIDにアクセスできるようにするにはどうすればよいですか?

enter image description here

+3

あなたはこれまで何をしているのか教えてください。いくつかのコードを追加します。 – SripadRaj

答えて

0

public void onAddField(View v) { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View rowView = inflater.inflate(R.layout.field, null); 
     // Add the new row before the add field button. 
     parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 6); 
    } 

新しいIDを作成することができ、動的に新しい行を追加するためのコードではなく、 ListView/RecyclerViewに項目を表示して、単に新しい項目をアダプタに追加し、アダプタに新しい項目を追加させるようにします。あなたはアイテムIDを気にする必要はなく、アダプタの位置をidとして単純に使うことができます。

ビューをプログラムで作成する場合は、View.setID()を呼び出すことでビューIDを割り当てることができます。これは任意のint(一意である必要があります)です。私はこの方法をお勧めしません。なぜなら、IDとアイテムに含まれるデータを追跡するのが非常に面倒になるからです。 RecyclerView/ListViewを使用すると、プレゼンテーションレイヤーからデータを分離することでこの問題が改善されます。

+0

入力いただきありがとうございます。正常に動作しています – John

0

これは、これを行うための最も簡単な方法field.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" >  
    <EditText 
     android:id="@+id/grid_Itemcode" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="3"  
     android:inputType="textPhonetic" 
     android:hint="Code" /> 
    <EditText 
     android:id="@+id/grid_itemdesc" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="5"   
     android:inputType="textPersonName" 
     android:hint="Description" /> 
    <EditText 
     android:id="@+id/grid_itemQty" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2"   
     android:inputType="phone" 
     android:hint="Qty" /> 
    <EditText 
     android:id="@+id/grid_itemRate" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2"   
     android:inputType="phone" 
     android:hint="Rate" /> 
<Button 
     android:id="@+id/delete_button" 
     android:layout_width="0dp" 
     android:layout_height="40dp" 
     android:layout_weight="1" 
     android:background="@android:drawable/ic_delete" 
     android:onClick="onDelete"/> 
</LinearLayout> 
関連する問題