Onitemclicklistener to open an Activity

Asked

Viewed 415 times

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.

1 answer

1


One simple way to solve is to add class of Activity with a class property 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 the object:

 employeeArrayList.add(new Pokemon1("Bulbasaur", "#001",BulbasaurActivity.class ));
  • Thanks man, it worked perfectly, and it’s pretty simple. very grateful :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.