Pergunta

i want to create a copy of my tablerow with its elements in it and the same params when a button clicked. is there any way to do this? my unsuccessful try was this: xml:

<TableRow 
        android:id="@+id/my_row"
        >


        <ImageView 
            android:layout_width="100dp"
            android:layout_height="95dp"
            android:src="@drawable/dalass"
            android:layout_weight="1"
            />
        <LinearLayout 
            android:orientation="vertical"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        <AutoCompleteTextView
                android:id="@+id/autocompletetextview"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text=""
                android:inputType="numberDecimal"
                 android:imeOptions="flagNoExtractUi"
                 android:textColor="#000000"
                 >
        </AutoCompleteTextView>
        <EditText 
            android:id="@+id/edit_sale2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:inputType="numberDecimal"
            android:imeOptions="flagNoExtractUi"
            />

       </LinearLayout>
    </TableRow>

java:

View.OnClickListener handler_add=new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        LayoutInflater inflator = Sale.this.getLayoutInflater();
                    TableRow rowView = (TableRow) inflator.inflate(R.layout.activity_sale, null);
        table_sale.addView(rowView);

    }

};

i dont know what the inflater exactly does. somebody help me please :)

Foi útil?

Solução

The second parameter of the LayoutInflater.inflate method, which you currently have set as null, is the root of the layout you want to inflate into. Try this;

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    LayoutInflater inflator = Sale.this.getLayoutInflater();
                TableRow rowView = new TableRow(v.getContext());
                inflator.inflate(R.layout.activity_sale, rowView);
    table_sale.addView(rowView);

}

You should also edit your xml so that it is merged into the rowView object, to avoid unnecessary additional views.

activity_sale.xml

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


    <ImageView 
        android:layout_width="100dp"
        android:layout_height="95dp"
        android:src="@drawable/dalass"
        android:layout_weight="1" />

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <AutoCompleteTextView
            android:id="@+id/autocompletetextview"
            android:layout_width="match_parent"  
            android:layout_height="50dp"
            android:text=""
            android:inputType="numberDecimal"
            android:imeOptions="flagNoExtractUi"
            android:textColor="#000000" >

        </AutoCompleteTextView>

        <EditText 
            android:id="@+id/edit_sale2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:inputType="numberDecimal"
            android:imeOptions="flagNoExtractUi" />

   </LinearLayout>
</merge>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top