2017-09-11 17 views
0

我有這個應用程序,它從MySql數據庫檢索數據並將其解析爲recyclerView。我有一個editText,我希望能夠在輸入時過濾recyclerView項目。使用EditText篩選RecyclerView

這裏是我的MainActivity(SearchAvtivity):

public class SearchActivity extends AppCompatActivity { 
 
    private static final String TAG = "Search Activity"; 
 
    private Context mContext = SearchActivity.this; 
 
    private static final int ACTIVITY_NUM = 1; 
 

 
    String urlAddress = "myDatabaseAddressGoesHere.PHP"; 
 

 

 
    @Override 
 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_search); 
 
     Log.d(TAG, "onCreate: started"); 
 
     setupBottomNavigationView(); 
 
     final RecyclerView rv = (RecyclerView) findViewById(R.id.SearchRv); 
 
     rv.setLayoutManager(new LinearLayoutManager(this)); 
 
     rv.setItemAnimator(new DefaultItemAnimator()); 
 
     Downloader dl = new Downloader(SearchActivity.this,urlAddress,rv); 
 
     dl.execute(); 
 
    } 
 
      
 
      
 

 
    /** 
 
    * Setup Bottom Navigation View 
 
    */ 
 

 
    private void setupBottomNavigationView(){ 
 
     Log.d(TAG, "setupBottomNavigationView: Setting up Bottom Navigation View"); 
 
     BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx)findViewById(R.id.bottom_nav_bar); 
 
     BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx); 
 
     BottomNavigationViewHelper.enableNavigation(mContext,bottomNavigationViewEx); 
 
     Menu menu = bottomNavigationViewEx.getMenu(); 
 
     MenuItem menuItem = menu.getItem(ACTIVITY_NUM); 
 
     menuItem.setChecked(true); 
 

 
    } 
 
}

這裏是MyAdapter:

public class MyAdapter extends RecyclerView.Adapter<MyHolder> { 
 

 
    Context c; 
 
    ArrayList<SpaceCraft> spaceCrafts; 
 
    List<MyHolder> displayedList; 
 

 
    public MyAdapter(Context c, ArrayList<SpaceCraft> spaceCrafts) { 
 
     this.c = c; 
 
     this.spaceCrafts = spaceCrafts; 
 
    } 
 

 
    @Override 
 
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_adapter_vertical,parent,false); 
 
     return new MyHolder(v); 
 
    } 
 

 
    @Override 
 
    public void onBindViewHolder(MyHolder holder, int position) { 
 
     holder.nameTv.setText(spaceCrafts.get(position).getName()); 
 
     holder.addressTv.setText(spaceCrafts.get(position).getAddress()); 
 
     holder.phoneTv.setText(spaceCrafts.get(position).getPhone()); 
 
     holder.emailTv.setText(spaceCrafts.get(position).getEmail()); 
 

 
    } 
 

 
    @Override 
 
    public int getItemCount() { 
 
     return spaceCrafts.size(); 
 
    } 
 
}

伊不知道您是否需要查看下載器,解析器,連接器或持有者。我所以請告訴我。

+0

可能的重複[Android - 實現搜索過濾器到RecyclerView](https://stackoverflow.com/questions/40754174/android-implementing-search-filter-to-a-recyclerview) – Raghavendra

回答

-1

你可以在你的editText上使用TextWatcher來完成它,並在它內部過濾你的列表。

例子:

((EditText) findViewById(R.id.your_edit_text)).addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     String text = s.toString(); 
     ArrayList<String> yourCopyList = new ArrayList<>(yourList) 
     yourList.clear(); 
     if (text.isEmpty()) { 
      addAllYourDataToList(); 
     } else { 
      text = text.toLowerCase(); 
      for (int i = 0; i<yourCopyList.size(); i++) { 
       // Adapt the if for your usage 
       if (yourCopyList.get(i).getName().toLowerCase().contains(text)) { 
        yourList.add("the string"); 
       } 
      } 
     } 
     adapter.notifyDataSetChanged(); 
    } 
}); 

在這個例子中,你刪除列表中的所有數據,並添加相應的只在文字過濾

+0

我應該在哪裏添加這個textWatcher ? – abe

+0

這是你的editText((EditText)findViewById(R.id.your_edit_text))並在它上面添加一個addTextChangedListener()這個方法需要一個textwatcher參數。我想你的editText在你的Activity裏面,所以把這段代碼放在活動 – Vodet

+0

中,我按照你說的做了,但是在最後一部分「yourList.add(」the String)「我得到錯誤:add(***** ********* SpaceCract)ArrayList不能應用於(java.lang.string) – abe

0

的數據,你應該使用一個TextWatcher()對於RecyclerView。