Search code examples
javaandroidlistviewitemstart-activity

How can I start new Activity based on the item clicked in listview?


I have been stuck on this for 2 days now, I don't see why the below code is not working, the problem seems to be the "if" block on the onItemClick, please I'lld really appreciate if anyone can help me out with this. Thanks.

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = parent.getItemAtPosition(position).toString();
                Toast.makeText(SearchActivity.this, name,Toast.LENGTH_LONG).show();
                if (name=="Sweet Tooth"){
                    Intent intent = new Intent(SearchActivity.this,SwitActivity.class);
                    startActivity(intent);
                }
            }
        });

Solution

  • Try this :

    String name = listview.getItemAtPosition(position).toString();
    

    And

    if (name.equals("Sweet Tooth")){
                        Intent intent = new Intent(SearchActivity.this,SwitActivity.class);
                        startActivity(intent);
                    }