Reputation: 483
in my application i have dynamic created buttons ( they are my categories ) and when i click on them i want to have sub-categories beneath them but they appear beneath all the categories , take a look at this photo
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;
}
<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>
<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>
<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>
and as you can see in the photo the problem is i want those two added SUB CATEGORY XML beneath the first CATEGORY button but i get them beneath all the CATEGORY buttons...
Upvotes: 0
Views: 29
Reputation: 4371
You should change the method as below:
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;
}
Upvotes: 1