2015-08-21 121 views
0

有沒有一種方法可以通過自定義類中的列表進行搜索?如果可能,我還想添加語音搜索。Android:通過自定義列表搜索

聲音類

  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

首先過濾列表類基於搜索的文本,並將其發送到適配器上的文字更改,重新使用新值列表 –

+0

如何?你可以給我一個例子 – EnderNicky

+1

@EnderNicky我更新了我的答案 – arun

回答

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

這是行不通的。我目前使用這種方法 – EnderNicky

+1

我更新了代碼 – arun

+0

我應該以任何方式覆蓋它嗎?或者我應該在哪裏打電話? – EnderNicky

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

nope。這不起作用。查看問題的答案以獲取更多信息 – EnderNicky