可搜索列表视图

Searchable List View

所以我正在尝试使用自定义数组适配器制作可搜索的列表视图。

我终于开始搜索了,但结果并不准确。

它实际上是一个命中注定的事情,例如搜索 Drupal 让我 Google 另外,你知道我在说什么吗?

这是我的适配器

public class CustomListAdapter extends ArrayAdapter<String> implements Filterable {

private final Activity context;
private final String[] web;
private final String[] imageIdUrl;


public CustomListAdapter(Activity context, String[] web, String[] imageId) {
    super(context, R.layout.list_single, web);
    this.context = context;
    this.web = web;
    this.imageIdUrl = imageId;

}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    //find row view
    View rowView= inflater.inflate(R.layout.list_single, null, true);
    //find text
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
    //find image
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
    //set text
    txtTitle.setText(web[position]);
    //set image traditional
    //imageView.setImageResource(imageIdUrl[position]);
    //set image with picasso
    Picasso.with(this.context).load(imageIdUrl[position]).into(imageView);
    //return row
    return rowView;
}
}

这是我的主要内容 Activity class

 public class MainActivity extends ActionBarActivity {

ListView list;
EditText editsearch;

String[] web = {
        "Google Plus",
        "Twitter",
        "Windows",
        "Bing",
        "Itunes",
        "Wordpress",
        "Drupal"} ;
String[] imageIdUrl = {
        "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
        "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg" ,
        "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
        "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
        "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
        "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
        "http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final CustomListAdapter adapter = new CustomListAdapter(MainActivity.this, web, imageIdUrl);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
        }
    });

    // Locate the EditText in listview_main.xml
    editsearch = (EditText) findViewById(R.id.editText);

    editsearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
            adapter.getFilter().filter(text);
        }
        @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
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

对于任何帮助,我将不胜感激。这被证明是一个令人头疼的问题

将 onTextChanged() 方法中的代码更改为 afterTextChanged() 方法,如下所示:

@Override
public void afterTextChanged(Editable s) {

    String str=s.toString();
    getSearchItems(str);
}

在 getSearchItems 中正确使用过滤器。我使用其他方式过滤为,

public void getSearchItems(String searchText){
    List<Employee> searchList=new ArrayList<Employee>();
    List<Employee> employeeList=getEmployeeList();
    if(employeeList2!=null){
        for(Employee e:employeeList){
          if(e.name.toLowerCase().contains(searchText.toLowerCase())){
            searchList.add(e);
          }
        }
    }
    adapter.refresh(searchList);
}

然后在适配器中使用另一种方法 refresh() 如下:

public void refresh(List<Employee> employeeList){
    this.employeeList=employeeList;
    notifyDataSetChanged();
}

实际上我使用了包含他的姓名和地址的员工 class,并且我使用员工姓名进行了搜索。所以你更改上面的代码以适合你的搜索。