Вставка динамических представлений в проблему Android - PullRequest
0 голосов
/ 21 июня 2019

в моем приложении у меня есть динамически созданные кнопки (это мои категории), и когда я нажимаю на них, я хочу иметь подкатегории под ними, но они появляются под всеми категориями, взгляните на эту фотографию : what i need

ФРАГМЕНТ ЯВА

public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_newfactor,container,false);
        ln = v.findViewById(R.id.itemsContainer);

        dbConnector = new DbConnector(getContext(),null,null,1);
        Cursor c = dbConnector.get().rawQuery("SELECT * FROM categories",null);



        while (c.moveToNext()){

            final int id = c.getInt(c.getColumnIndex("id"));
            String name = c.getString(c.getColumnIndex("name"));

            final View rowView = inflater.inflate(R.layout.field_button, null);
            Button fieldCreator = rowView.findViewById(R.id.fieldCreator);
            fieldContainer = rowView.findViewById(R.id.field_container);
            //fieldCreator.setText(name);
            fieldCreator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor c = dbConnector.get().rawQuery("SELECT * FROM product WHERE category_id = " + id,null);

                    while (c.moveToNext()){
                        final View rowView = inflater.inflate(R.layout.field, null);
                        fieldContainer.addView(rowView);

                    }


                }
            });

            ln.addView(rowView);

        }




        return v;
    }

ФРАГМЕНТ XML

<ScrollView
        android:layout_below="@id/customerName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="30dp">

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



        </LinearLayout>

    </ScrollView>

КНОПКА ДОБАВЛЕННОЙ ДИНАМИКИ (Категория) XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <Button
        android:id="@+id/fieldCreator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Category"
        />
    <LinearLayout
        android:id="@+id/field_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/fieldCreator"
        android:orientation="vertical"
        >

    </LinearLayout>

</RelativeLayout>

ДИНАМИЧЕСКАЯ ПОДРАЗДЕЛИТЕЛЬНАЯ КАТЕГОРИЯ XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    android:weightSum="3">

    <Button
        android:id="@+id/clear"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/ic_clear_blackk_24dp" />
    <TextView
        android:id="@+id/product_price"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text=""
        android:layout_weight="1"
        android:textAlignment="center"
        android:gravity="center_vertical"
        />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.7"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/add"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:background="@drawable/ic_add_circle_black_24dp"
            android:onClick="onIncrease" />

        <Button
            android:id="@+id/remove"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:background="@drawable/ic_remove_circle_black_24dp"
            android:onClick="onDecrease" />
    </LinearLayout>

    <EditText
        android:id="@+id/number"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:hint="@string/number"
        android:inputType="number"
        android:text="0"
        android:textAlignment="center" />

    <TextView
        android:id="@+id/product_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="Sub category"
        android:textAlignment="center"
        android:textSize="15sp"

        />

</LinearLayout>

и, как вы можете видеть на фотографии, проблема в том, что я хочу, чтобы эти два добавили SUB CATEGORY XML под первой кнопкой CATEGORY, но я получаю их под всеми кнопками CATEGORY ...

1 Ответ

1 голос
/ 21 июня 2019

Вы должны изменить метод, как показано ниже:

public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_newfactor,container,false);
        ln = v.findViewById(R.id.itemsContainer);

        dbConnector = new DbConnector(getContext(),null,null,1);
        Cursor c = dbConnector.get().rawQuery("SELECT * FROM categories",null);



        while (c.moveToNext()){

            final int id = c.getInt(c.getColumnIndex("id"));
            String name = c.getString(c.getColumnIndex("name"));

            final View rowView = inflater.inflate(R.layout.field_button, null);
            Button fieldCreator = rowView.findViewById(R.id.fieldCreator);

            //fieldCreator.setText(name);
            fieldCreator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LinearLayout fieldContainer = ((RelativeLayout)v.getParent()).findViewById(R.id.field_container);
                    fieldContainer.removeAllViews();
                    Cursor c = dbConnector.get().rawQuery("SELECT * FROM product WHERE category_id = " + id,null);

                    while (c.moveToNext()){
                        final View rowView = inflater.inflate(R.layout.field, null);
                        fieldContainer.addView(rowView);

                    }


                }
            });

            ln.addView(rowView);

        }




        return v;
    }
...