OnItemClickListener to open an activity

1

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.

    
asked by anonymous 25.08.2017 / 22:40

1 answer

1

A simple way to solve is to add class of Activity to a property of class Pokemon1

Example:

Pokemon1.java

public class Pokemon1 {

    public Pokemon1(final String nome, final String numero, final Class<?> activity){
        this.nome = nome;
        this.numero = numero;
        this.activity = activity;
    }

    private String nome;
    private String numero; 
    /**
     * Activity que vamos abrir ao clicar!
     */
    private Class<?> activity;

    public Class<?> getActivity() {
        return activity;
    }
    public void setActivity(Class<?> activity) {
        this.activity = activity;
    }
    public String getNumero() {
        return numero;
    }
    public void setNumero(String numero) {
        this.numero = numero;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
}

Click:

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();

        /**
         * Vamos pegar a Activity do objeto
         */
        Intent myIntent1 = new Intent(view.getContext(), pick.getActivity());
        myIntent.putExtra("position", position);
        startActivityForResult(myIntent1, 0);
      }
}

Add object:

 employeeArrayList.add(new Pokemon1("Bulbasaur", "#001",BulbasaurActivity.class ));
    
25.08.2017 / 23:00