onListItemClick是可点击的,但不会转到另一个活动

时间:2015-09-16 07:30:20

标签: android android-studio listactivity

单击项目时,我无法进入其他活动。 这是我使用的代码:

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;
    }
}

这是我用于项目点击的代码。搜索工作正常,但是当我单击要搜索的项时,似乎无法从我的数组字符串中获取字符串。

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

}

1 个答案:

答案 0 :(得分:0)

我认为你可以通过删除

来解决这个问题
selected.trim()

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

它将简单地匹配它是否包含指定的字符串。所以你不必担心空间等。