[android]onListItemClick 是可点击的但不是会到另一个activity

发布时间: 2016/6/26 13:13:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我不能去到另一个 activity 当我点击一个项目。 这是我使用的代码 ︰

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:

我认为你可以解决这个,是否它不工作通过删除

selected.trim()

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

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

官方微信
官方QQ群
31647020