Reputation: 322
Is there a way where I can search through a list with custom Class in it? Also I would like to add voice search if possible.
The sounds class
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;
}
}
Short explanation - This is sort of a soundboard and I wan to user to be able to search both with the soundExtras and the soundName (I am using the action bar search widget to search through the list)
Thanks in advance!
Upvotes: 0
Views: 62
Reputation: 12953
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
}
});
Upvotes: 0
Reputation: 1758
yes its possible
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;
}
});
in your 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();
}
Upvotes: 1