I'm having the following problem, I'm using a list view and a search view, and when I click on an item, I'd like to open an activity and display a toast. So far so good, when I search and click on item 2 instead of opening activity "2" from id "2" for example it opens 1, because item 2 is first in the list due to search view result. My toast is working normally, just to open the activity I'm having problems.
MainActivity
public class MediaBall extends AppCompatActivity implements SearchView.OnQueryTextListener {
private SearchView mSearchView;
private ListView mListView;
private ArrayList<Pokemon1> employeeArrayList;
private PokemonAdapter1 employeeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_ball);
mSearchView = (SearchView) findViewById(R.id.search15);
mListView = (ListView) findViewById(R.id.pokemons);
employeeArrayList = new ArrayList<Pokemon1>();
employeeArrayList.add(new Pokemon1("Bulbasaur", "#001", ));
employeeArrayList.add(new Pokemon1("Ivysaur", "#002", ));
employeeArrayList.add(new Pokemon1("Venosaur", "#003", ));
employeeAdapter=new PokemonAdapter1(MediaBall.this, employeeArrayList);
mListView.setAdapter(employeeAdapter);
mListView.setTextFilterEnabled(true);
setupSearchView();
registerClickCallback();
}
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.pokemons);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Pokemon1 pick = employeeArrayList.get(position);
Pokemon1 pick = (Pokemon1) employeeAdapter.getItem(position);
String message = "You Selected " + pick.getName();
if (position == 0)
{
Intent myIntent1 = new Intent(view.getContext(), IvysaurActivity.class);
myIntent.putExtra("position", position);
startActivityForResult(myIntent1, 0);
}
if (position == 1)
{
Intent myIntent1 = new Intent(view.getContext(), VenusaurActivity.class);
startActivityForResult(myIntent1, 0);
}
if (position == 3)
{
Intent myIntent1 = new Intent(view.getContext(), BulbasaurActivity.class);
startActivityForResult(myIntent1, 0);
}
Toast.makeText(MediaBall.this, message, Toast.LENGTH_SHORT).show();
}
});
}
private void setupSearchView()
{
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Pesquise Aqui!!");
}
@Override
public boolean onQueryTextChange(String newText)
{
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText);
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
return false;
}
}
Thank you in advance.