0

The following code works well:

    listView = findViewById(R.id.my_list);

    String[] values = new String[]{
            "TRT 1", "TRT Haber"
    };
    listView.setAdapter(arrayAdapter);

    values = new ArrayList<>(Arrays.asList(values)).toArray(new String[0]);
    arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1,values);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener((parent, view, position, id) -> {
        if (position == 0) {
            Intent intent = new Intent(view.getContext(), Activity1.class);
            startActivity(intent);
        }
        if (position == 1) {
            Intent intent = new Intent(view.getContext(), Activity2.class);
            startActivity(intent);
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main2, menu);
    MenuItem menuItem = menu.findItem(R.id.search2);
    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setQueryHint("Search channels");

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            SearchActivity.this.arrayAdapter.getFilter().filter(s);
            return true;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

When I search for an item in the list view it becomes the first item in the list (Which I want to happen) but if the second item in the list changes positions it opens the wrong activity. I know this is because of the if statement and position as follows, but I’ve been stuck on how to fix it for about a day now...

    listView.setOnItemClickListener((parent, view, position, id) -> {
        if (position == 0) {
            Intent intent = new Intent(view.getContext(), Activity1.class);
            startActivity(intent);
        }
        if (position == 1) {
            Intent intent = new Intent(view.getContext(), Activity2.class);
            startActivity(intent);
        }
    });
}

Any help is much appreciated. *No errors are shown

  • Can you explain a lit bit about on what basis you want to open Activity1 and activity2 ?? – Kishan Maurya Mar 08 '21 at 18:13
  • Activity 1 when TRT 1 is clicked (First on list) and Activity 2 when TRT Haber is clicked(Second on list). But when I search for one it becomes first on list and goes to Activity 1 no matter the item. – Code master123 Mar 08 '21 at 21:49

1 Answers1

1

As of now for your case, this will work.

listView.setOnItemClickListener((parent, view, position, id) -> {
            String item = ((TextView)view).getText().toString();
            Intent intent = null;
            switch (item) {
                case "TRT 1":
                    intent = new Intent(view.getContext(), Activity1.class);
                    break;
                case "TRT Haber":
                    intent = new Intent(view.getContext(), Activity2.class);
                    break;
            }
            startActivity(intent);
        });

......................................................

There are other ways to achieve this behaviour.

  1. Array Adapter with Model class ( data class ) https://stackoverflow.com/a/27475573/8007341

    Pass unique id for each model class you add. This will work as identifier for you row. Now based on this id, you can open desired activity.

  2. Check for Recyclerview implementation.

Kishan Maurya
  • 3,356
  • 8
  • 21