无法从动态LinearLayout表中获取正确的ID

时间:2018-09-01 11:28:57

标签: android

我正在尝试创建一个动态视图,在该视图中将保留一个按钮以将视图添加到LinearLayout中。该视图已成功添加。

我有一个微调器,并且带有(-)(+)按钮增加的计数字段减小了数值。

问题::当我增加第二行或第三行的值时,第一行的值正在增加。

例如:如果我们尝试增加裤子的数量,那么衬衫的数量就会增加。裤数不变(0)。

从模拟器中查看以更好地理解:

enter image description here

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="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >
    <Spinner
        android:id="@+id/type_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:entries="@array/itemTypes"
        android:gravity="right"/>
    <Button
        android:id="@+id/decrease"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/round_button"
        android:onClick="decreaseInteger"
        android:text="-" />
    <TextView
        android:id="@+id/integer_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:textStyle="bold"/>
    <Button
        android:id="@+id/increase"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/round_button"
        android:onClick="increaseInteger"
        android:text="+"/>
</LinearLayout>

</LinearLayout>

item_quantity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_quantity_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >
        <Spinner
            android:id="@+id/type_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:entries="@array/itemTypes"
            android:gravity="right"/>
        <Button
            android:id="@+id/decrease"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/round_button"
            android:onClick="decreaseInteger"
            android:text="-" />
        <TextView
            android:id="@+id/integer_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:textStyle="bold"/>
        <Button
            android:id="@+id/increase"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/round_button"
            android:onClick="increaseInteger"
            android:text="+"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add more item"/>
        <Button
            android:id="@+id/add_field_button"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/round_orange_button"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:onClick="onAddField"
            android:textColor="#FFF"
            android:text="+"/>
    </LinearLayout>

</LinearLayout>

JavaCode

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.laundry_srf_blank);

        parentLinearLayout = (LinearLayout) findViewById(R.id.item_quantity_view);

    }

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

    public void increaseInteger(View view) {
        minteger = minteger + 1;
        display(minteger);
    }

    public void decreaseInteger(View view) {
        minteger = minteger - 1;
        display(minteger);
    }

    private void display(int number) {
        TextView displayInteger = (TextView) findViewById(
                R.id.integer_number);
        displayInteger.setText("" + number);
    }

请帮助!!!

1 个答案:

答案 0 :(得分:0)

您的问题是,您在R.id.integer_number方法的根布局中寻找private void display(int number),并且它总是返回可以找到的第一个视图(在您的情况下为Shirt字段)。因此,您需要在调用onClick()方法的当前ViewGroup中本地寻找R.id.integer_number

像这样重构代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.laundry_srf_blank);

    parentLinearLayout = (LinearLayout) findViewById(R.id.item_quantity_view);

}

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

public void increaseInteger(View view) {
    TextView displayInteger = (TextView) view.findViewById(
            R.id.integer_number);
    Integer minteger = Integer.valueOf(displayInteger.getText().toString());
    minteger++;
    displayInteger.setText(String.valueOf(minteger));
}

public void decreaseInteger(View view) {
    TextView displayInteger = (TextView) view.findViewById(
            R.id.integer_number);
    Integer minteger = Integer.valueOf(displayInteger.getText().toString());
    minteger--;
    displayInteger.setText(String.valueOf(minteger));
}