Have problem after search in searcview last or middle activity in listview

52 Views Asked by At

Have problem after search in searcview last or middle activity in listview.

public class MainActivity extends AppCompatActivity {

    ListView listView;
    SearchView searchView;
  
    ArrayList<String> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listview);
        
        items = new ArrayList<>();
        items.add("st17");
        items.add("st18");
        items.add("st23");
        items.add("st29");
        items.add("st33");
        items.add("st34");
        items.add("st35");
       
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ArrayAdapter<String> adapter = new ArryAdapter<>(this,android.R.layout.simple_list_item_1,
                        android.R.id.text1, items);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener((adapterView, view, position, l) -> {
            Toast.makeText(MainActivity.this, "click -" + adapterView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();

            switch (position) {
                case 0:
                    Intent newActivity = new Intent(MainActivity.this, Activity_st17.class);
                    startActivity(newActivity);
                    break;
                case 1:
                    Intent newActivity1 = new Intent(MainActivity.this, Activity_st18.class);
                    startActivity(newActivity1);
                    break;
                case 2:
                    Intent newActivity2 = new Intent(MainActivity.this, Activity_st23.class);
                    startActivity(newActivity2);
                    break;
                case 3:
                    Intent newActivity3 = new Intent(MainActivity.this, Activity_st29.class);
                    startActivity(newActivity3);
                    break;
                case 4:
                    Intent newActivity4 = new Intent(MainActivity.this, Activity_st31.class);
                    startActivity(newActivity4);
                    break;
                case 5:
                    Intent newActivity5 = new Intent(MainActivity.this, Activity_st32.class);
                    startActivity(newActivity5);
                    break;
                case 6:
                    Intent newActivity6 = new Intent(MainActivity.this, Activity_st33.class);
                    startActivity(newActivity6);
                    break;
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextChange(String newText) {

                ArrayList<String> templist = new ArrayList<>();

                for (String temp : items) {
                    if (temp.toLowerCase().contains(newText.toLowerCase())) {
                        templist.add(temp);
                    }
                }
                
                ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
                        android.R.layout.simple_list_item_1, templist);
                listView.setAdapter(adapter);

                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {

                return false;
            }
        });
        }
    }

I have list:

st17 st18 st23 st29 st33 st34 st35

For example, I'm looking for st33 in the search, then it shows st33 it's good, but when I click it, opens st17.

But this shows that I click on st33 but still, after pressing, it still opens st17

Toast.makeText(MainActivity.this, "click -" + adapterView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();

Help me please.

0

There are 0 best solutions below