Reputation:
I have an overflow menu and one of the options is 'delete list'. When the user presses it, I want it so all the elements that the user added onto the screen, disappear/go 'gone'.
when I run my code though, that doesn't happen. the first time I press 'delete list', it deletes one set of elements, then I press delete list again, and nothing disappears.
Is my logic correct here?
Please see the section else if (id == R.id.action_delete)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent startSettingsActivity = new Intent(this, settings.class);
startActivity(startSettingsActivity);
//start the settings activity here.
return true;
}
else if (id == R.id.action_delete) {
//code to delete list here.
final int listSize = mViews.size();
if (listSize > 0) {
for (int i = 0; i < listSize; i++) {
mCheckboxes.get(listSize - 1).setVisibility(View.GONE);
mSpinners.get(listSize - 1).setVisibility(View.GONE);
mViews.get(listSize - 1).setVisibility(View.GONE);
mTextviews.get(listSize - 1).setVisibility(View.GONE);
mEdittexts.get(listSize - 1).setVisibility(View.GONE);
Toast.makeText(getBaseContext(), "List removed.", Toast.LENGTH_SHORT).show();
}
}
return true;
}
return super.onOptionsItemSelected(item);
}
create xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:minHeight="170dp"
tools:context=".create"
tools:layout_editor_absoluteY="81dp"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="438dp"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/verticalLayout"
android:layout_below="@id/view2">
</LinearLayout>
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="60dp"
android:layout_height="70dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="8dp"
android:src="@android:drawable/ic_input_add"
app:backgroundTint="@color/colorCreate"
app:elevation="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:pressedTranslationZ="12dp"
android:tint="@color/colorBackground"/>
<View
android:id="@+id/subheading"
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="2dp"
android:background="@color/colorBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
/>
<View
android:id="@+id/view"
android:layout_width="320dp"
android:layout_height="1dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="76dp"
android:background="@color/colorText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view2"
android:layout_width="320dp"
android:layout_height="1dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:background="@color/colorText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:fontFamily="@font/droid_sans"
android:text="@string/done_label"
android:textColor="@color/colorText"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/view2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="128dp"
android:layout_marginRight="128dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/droid_sans"
android:text="@string/aisle_label"
android:textColor="@color/colorText"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view2"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/droid_sans"
android:text="@string/qty_label"
android:textColor="@color/colorText"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toStartOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/view2"
app:layout_constraintVertical_bias="0.7" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/droid_sans"
android:text="@string/item_label"
android:textColor="@color/colorText"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toStartOf="@+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view2"
app:layout_constraintVertical_bias="1.0" />
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
</android.support.constraint.ConstraintLayout>
Upvotes: 2
Views: 253
Reputation: 127
else if (id == R.id.action_delete) {
for(int j=0; j<mCheckboxes.size(); j++){
mCheckboxes.get(j).setVisibility(View.GONE);
}
for(int j=0; j<mSpinners.size(); j++){
mSpinners.get(j).setVisibility(View.GONE);
}
for(int j=0; j<mViews.size(); j++){
mViews.get(j).setVisibility(View.GONE);
}
for(int j=0; j<mTextviews.size(); j++){
mTextviews.get(j).setVisibility(View.GONE);
}
for(int j=0; j<mEdittexts.size(); j++){
mEdittexts.get(j).setVisibility(View.GONE);
}
Toast.makeText(getBaseContext(), "List removed.", Toast.LENGTH_SHORT).show();
}
it will delete all the view from list. there is no need to calculate mViews.size().
Upvotes: 0
Reputation: 1710
It is very simple
assign an ID to this LinearLayout
(parent of your Views items like spinner,check box as from your code)
then get the layout in your code as
Linearlayout ll = findViewById(R.id.yourLinearlayoutID);
then you need to call this line in on pressing the delete
else if (id == R.id.action_delete) {
//code to delete list here.
ll.setVisibilty(View.GONE); // i am supposed that the for loop you added to remove multiple values if not then add them back
Toast.makeText(getBaseContext(), "List removed.", Toast.LENGTH_SHORT).show();
}
}
return true;
}
Hope helping
Upvotes: 2