I am new at programming and have been looking for a solution to my problem for days.
I created a listview with searchview, then I added the checkboxes. The problem with the checkboxes is that they don't keep their position when you search for something. For example: If I have 5 words.
Hello
you
are
here
to
and I check the first word and search for to , to is checked because it is then on the first position.
How can I set that not the positions but the names are checked? I was thinking if you can do this with an array, when you check something it goes into the array and stays there as checked and when you uncheck it goes back into the other array. Unfortunately I don't know exactly how to do that. (I have already tried around)
Maybe you can also assign ID's to the positions but I don't know if there is another solution than to give each position an ID because it should be more than 200 positions later.
For the checkbox i use ( android:choiceMode="multipleChoice" ) in Listview Some Imports are for other functions in the code. (toolbar,drawer,etc.)
Here are my codes:
StorageActivity.java
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.icu.text.Transliterator;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import java.lang.reflect.Array;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
public class StorageActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
ListView listView;
String[] spices = {"Ajowan","Anis","Annatto","Bergkümmel","Betram","Brotklee","Eberraute","Essigbaum","Harissa","Herbes","Ingwer","Knoblauch","Koriander","Mandel"};
SearchView searchView;
ArrayList<String> selected = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_storage);
listView = findViewById(R.id.lvspices);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,spices);
listView.setAdapter(arrayAdapter);
searchView = findViewById(R.id.search_spices);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
StorageActivity.this.arrayAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
StorageActivity.this.arrayAdapter.getFilter().filter(newText);
return false;
}
});
activity_storage.xml
<androidx.drawerlayout.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:context=".StorageActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/home_screen"
android:textAppearance="@style/ToolBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/notiButton"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.454" />
<ImageButton
android:id="@+id/notiButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/ic_notifications"
android:contentDescription="NULL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<androidx.cardview.widget.CardView
android:id="@+id/notification_circle"
android:layout_width="10dp"
android:layout_height="10dp"
android:visibility="visible"
app:cardBackgroundColor="@color/red"
app:cardCornerRadius="50dp"
app:layout_constraintEnd_toEndOf="@+id/notiButton"
app:layout_constraintTop_toTopOf="@+id/notiButton">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/notification_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.SearchView
android:id="@+id/search_spices"
app:queryHint="Search Spice"
app:iconifiedByDefault="false"
android:layout_margin="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ebecf0">
</androidx.appcompat.widget.SearchView>
<ListView
android:id="@+id/lvspices"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:background="@color/beige"
tools:listitem="@layout/support_simple_spinner_dropdown_item"/>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Thanks in advance for your help
Aucun commentaire:
Enregistrer un commentaire