有时搜索视图在listview中无法正常工作

时间:2015-07-27 07:10:25

标签: android

我在listview中有酒店菜单项。在dat listview中我有带搜索功能的edittext。我必须按项目名称和区域搜索酒店项目。工作正常。但有时在搜索时按区域显示它没有显示任何内容。如果我重新打开该应用程序并且搜索意味着它正常工作。我不知道错误发生在哪里。请帮助我....

搜索代码

       mSearch.addTextChangedListener(new TextWatcher() {

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

        // TODO Auto-generated method stub

        String text = mSearch.getText().toString().toLowerCase();

         arrayTemplist= new ArrayList<HotelItem>();


            for (int i = 0; i < searchItems.size(); i++)


            {
  //                     String currentString =hotelItems.get(i).getArea();
  //                     String currentString1 =hotelItems.get(i).getName();

  //                    String presentString=     (currentString+currentString1).toLowerCase();
                  String currentString =searchItems.get(i).getName();
                  String currentStringg =searchItems.get(i).getArea();

                  String presentString=currentString.toLowerCase();
                  String presentString1=currentStringg.toLowerCase();

                //if (text.equalsIgnoreCase(currentString))

                if(presentString.contains(text))
                {
                    arrayTemplist.add(searchItems.get(i));
                }

                if(presentString1.contains(text))
                {
                    arrayTemplist.add(searchItems.get(i));
                }
            }


               searchAdapter = new HotelsSearchAdapter(HotelsActivity.this,           arrayTemplist);

                listView.setAdapter(searchAdapter);



    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});




    //code for listview item selection 

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            // TODO Auto-generated method stub
            String search =mSearch.getText().toString();

            if(search.isEmpty()==true){
            String SelectedId = hotelItems.get(position).getId();

           //                  Toast.makeText(getApplicationContext(), "HotelId"+SelectedId,    3000).show();
                System.out.println("hotel id ..........."+SelectedId);
                String SelectedHotel = hotelItems.get(position).getName();
                String Area =hotelItems.get(position).getArea();


              Intent id1 = new Intent(getApplicationContext(),
                    MainActivity.class);
            id1.putExtra("selectedId", SelectedId);
            id1.putExtra("selectedHotel", SelectedHotel);


            startActivity(id1);
            }


            if(search.isEmpty()!=true){

                String SearchedId = arrayTemplist.get(position).getId();

                String SelectedHotel = arrayTemplist.get(position).getName();
                String Area =arrayTemplist.get(position).getArea();


                Intent id1 = new Intent(HotelsActivity.this,
                        MainActivity.class);
                id1.putExtra("selectedId", SearchedId);
                id1.putExtra("selectedHotel", SelectedHotel);
                id1.putExtra("area", Area);

                startActivity(id1);

            }

        }
    });

//清除搜索文本

    btnSearch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            mSearch.setText(" ");

        }
    });

0 个答案:

没有答案