Search code examples
androidandroid-studiolistactivity

onListItemClick is clickable but not going to another activity


I can't go to another activity when I click an Item. This is the code that I used:

public class SearchCustomListViewActivity extends ListActivity{


ArrayList<HashMap<String, Object>> searchResults;


ArrayList<HashMap<String, Object>> originalValues;
LayoutInflater inflater;

ListView HRListView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.search_custom_list_view);
    String names[]= new String[]{
            "Artina Suites Hotel",
            "Casa Bocobo Hotel",
            "Hotel H2O",
            "Hotel Jen Manila",
            "Luxent Hotel",
            "Oakwood Premier Joy-Nostalg Center Manila",
            "Sofitel Philippine Plaza Manila",
            "The Bayleaf"
    };
    final EditText searchBox=(EditText) findViewById(R.id.searchBox);
    HRListView=(ListView) findViewById(android.R.id.list);


    inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    Integer[] photos={R.drawable.man_ho,R.drawable.man_pen,
            R.drawable.bag_aza,R.drawable.bor_fri,
            R.drawable.dav_mar,R.drawable.pal_pab,
            R.drawable.man_pen,R.drawable.man_ho};

    originalValues=new ArrayList<HashMap<String,Object>>();


    HashMap<String , Object> temp;


    int noOfPlayers=names.length;


    for(int i=0;i<noOfPlayers;i++)
    {
        temp=new HashMap<String, Object>();

        temp.put("name", names[i]);
        temp.put("photo", photos[i]);

        //add the row to the ArrayList
        originalValues.add(temp);
    }

    searchResults=new ArrayList<HashMap<String,Object>>(originalValues);


    final CustomAdapter adapter=new CustomAdapter(this, R.layout.list_layout,searchResults);


    HRListView.setAdapter(adapter);

    searchBox.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {

            String searchString = searchBox.getText().toString();
            int textLength = searchString.length();
            searchResults.clear();

            for (int i = 0; i < originalValues.size(); i++) {
                String playerName = originalValues.get(i).get("name").toString();
                if (textLength <= playerName.length()) {

                    if (searchString.equalsIgnoreCase(playerName.substring(0, textLength)))
                        searchResults.add(originalValues.get(i));
                }
            }

            adapter.notifyDataSetChanged();
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }

        public void afterTextChanged(Editable s) {

        }
    });

    }


    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
{

    public CustomAdapter(Context context, int textViewResourceId,
                         ArrayList<HashMap<String, Object>> Strings) {


        super(context, textViewResourceId, Strings);
    }

    private class ViewHolder
    {
        ImageView photo;
        TextView name,team;
    }

    ViewHolder viewHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView==null)
        {
            convertView=inflater.inflate(R.layout.list_layout, null);
            viewHolder=new ViewHolder();

            viewHolder.photo=(ImageView) convertView.findViewById(R.id.photo);
            viewHolder.name=(TextView) convertView.findViewById(R.id.name);


            convertView.setTag(viewHolder);

        }
        else
            viewHolder=(ViewHolder) convertView.getTag();


        int photoId=(Integer) searchResults.get(position).get("photo");

   viewHolder.photo.setImageDrawable(getResources().getDrawable(photoId));
        viewHolder.name.setText(searchResults.get(position).get("name").toString());


        return convertView;
    }
}

This is the code i used for item click. The search is working but when I click the item that I want to search it seems that it can't get a string from my array strings.

 protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

   String selected = HRListView.getItemAtPosition(position).toString();

   if (selected.trim().equals("Artina Suites Hotel")) {
       Intent startClass = new Intent(getApplicationContext(), Artina.class);
       startActivity(startClass);
   }
   if (selected.trim().equals("Casa Bocobo Hotel")) {
        Intent startClass = new Intent(getApplicationContext(), Casa.class);
       startActivity(startClass);
   }
   if (selected.trim().equals("Hotel H2O")) {
        Intent startClass = new Intent(getApplicationContext(), Hotel.class);
       startActivity(startClass);
   }

}


Solution

  • I think you can solve this if its not working by removing

    selected.trim()
    

    with

    selected.contains(("Artina Suites Hotel"); etc
    

    It will simply match whether it contains the specified string or not. so you dont have to worry about spaces etc.