有没有办法可以搜索包含自定义类的列表?另外,如果可能的话,我想添加语音搜索。
声音等级
private int _id;
private String _productname;
private String _extra;
private int _image;
public SoundListed() {
}
public SoundListed(int id, String productname, String extra, int image) {
this._id = id;
this._productname = productname;
this._extra = extra;
this._image=image;
}
public SoundListed( String productname, String extra, int image) {
this._productname = productname;
this._extra = extra;
this._image=image;
}
public void setID(int id) {
this._id = id;
}
public int getID() {
return this._id;
}
public int getImage(){return this._image;}
public void setProductName(String productname) {
this._productname = productname;
}
public String getProductName() {
return this._productname;
}
public void setExtra(String extra) {
this._extra = extra;
}
public String getExtra() {
return this._extra;
}
And the adapter:
public class soundAdapter extends ArrayAdapter{
private Context context;
private List <SoundListed> listSounds;
public soundAdapter(Context context, List<SoundListed> friends) {
super(context, R.layout.customrow, friends);
this.context=context;
this.listSounds=friends;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.soundrow,null);
}
SoundListed s = listSounds.get(position);
TextView soundName = (TextView)convertView.findViewById(R.id.nameOfFile);
TextView soundExtras = (TextView)convertView.findViewById(R.id.extraText);
soundName.setText(s.getProductName());
soundExtras.setText(s.getExtra());
return convertView;
}
}
简短说明 - 这是一种音板,我想用户能够使用soundExtras和soundName搜索(我使用操作栏搜索小部件来搜索列表)
提前致谢!
答案 0 :(得分:1)
是的可能
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String search) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String search) {
// TODO Auto-generated method stub
//if you use ArrayAdapter
// MainActivity.this.soundadapter.getFilter().filter(search);
//custom adapter you need to add custom filter
MainActivity.this.soundadapter.filter(search);
return true;
}
});
你的soundAdapter中的
private Context context;
private List <SoundListed> listSounds;
//you need temp list of data for your feature use
private List <SoundListed> listSoundsTemp;
public soundAdapter(Context context, List<SoundListed> friends) {
super(context, R.layout.customrow, friends);
this.context=context;
this.listSounds=friends;
this.listSoundsTemp=friends;
}
void filter(String search)
{
if(search=="")
listSounds = listSoundsTemp;
else
{
List <String> listClone = new ArrayList<String>();
for (SoundListed list: listSoundsTemp) {
if(s.getProductName().matches("(?i)("+search+").*") || s.getExtra().matches("(?i)("+search+").*")){
listClone.add(list);
}
}
listSounds= listClone;
}
notifyDataSetChanged();
}
答案 1 :(得分:0)
EditText inputSearch;//your search box
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs);
//Use your adapter object name instead adapter
}
@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
}
});