listview android中的搜索功能无法正常工作?

时间:2014-06-17 10:34:13

标签: android listview search

我想在我的Listview上创建搜索功能,在我的列表中它包括listview图像,listview标题和listview描述但我想在标题和描述上应用搜索功能而不是在图像上,它怎么可能? 纠正我,如果我错了....我的适配器和搜索代码如下:

ArrayAdapter<String> adapter;
  adapter = new ArrayAdapter(this, R.layout.single_row_fellow_student,
      R.id.textView1, studentTitles) ;
  list.setAdapter(adapter);
   // Enabling Search Filter

     inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MyFellowStudent.this.adapter.getFilter().filter(cs);    
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
        // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });
  class StudentAdapter extends ArrayAdapter<String> 
        {
Context context;
String[] titleArray;
String[] descriptionArray;


int[] images;

StudentAdapter(Context c, String[] titles , int[] imgs , String[] desc) {
    super(c, R.layout.single_row_fellow_student, R.id.textView1, titles );
    this.context = c;
    this.images = imgs;
    this.titleArray = titles;
    this.descriptionArray = desc;
}

2 个答案:

答案 0 :(得分:0)

 @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
           int textlength = cs.length();
           ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
           for(ContactStock c: arraylist){
              if (textlength <= c.getName().length()) {
                 if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
                    tempArrayList.add(c);
                 }
              }
           }
           mAdapter = new ContactListAdapter(activity, tempArrayList);
           lv.setAdapter(mAdapter);
     }

答案 1 :(得分:0)

您可以在afterTextChanged()

中尝试此操作
@Override
public void afterTextChanged(Editable s) {
   // TODO Auto-generated method stub
   if (inputSearch.getText().length() != 0) {
      String searchText = inputSearch.getText().toString();
      setSearchResult(searchText);
   } else {
        adapter = new ArrayAdapter(this, R.layout.single_row_fellow_student, R.id.textView1, studentTitles);
        list.setAdapter(adapter);
   }
}

然后,

public void setSearchResult(String searchText) {
   newSearchList = new ArrayList<MyFellowStudent>;
   for (MyFellowStudent info : list) {
      if (info.getName().toLowerCase().contains(searchText.toLowerCase())) {
         newSearchList.add(info);
         adapter = new ArrayAdapter(this, R.layout.single_row_fellow_student, R.id.textView1, newSearchList);
      }
   }
}