1
I have the following problem, I am using a list view and a search view, and when I click on an item, I would like to open an Activity and show a Toast. So far so good, when I search and click on item 2 instead of opening the Activity "2" id "2" for example it opens to 1, because item 2 is first in the list due to the search view result. My Oast 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;
}
}
Thanks in advance.
Thanks man, it worked perfectly, and it’s pretty simple. very grateful :)
– Erik Grupioni