【问题标题】:Searchable List View可搜索的列表视图
【发布时间】:2015-08-05 07:58:43
【问题描述】:

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

我终于可以进行搜索,但结果并不完全准确。

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

这是我的适配器

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

这是我的主要活动课程

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

我将非常感谢您的任何帮助。事实证明这是一个令人头疼的问题

【问题讨论】:

    标签: android android-listview listadapter


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2011-04-02
      • 2011-07-13
      • 1970-01-01
      相关资源
      最近更新 更多