翻譯使用「谷歌翻譯」!Android。搜索和過濾RecyclerView
簡要說明這個問題。我決定嘗試使用RecyclerView。就緒的解決方案,如搜索listView,沒有必要實現所有。所以我找到了一個解決方案,如果需要在源代碼的底部,但有一個問題。 有一個列表,它顯示標題,當你點擊第二回合Activiti中已經有一個標題和完整描述的任何項目。 意識到我喜歡: 在MainActivity我(用糖ORM)報頭和標識
ArrayList<String> arrTitle = new ArrayList<>();
for(Contact contact:allContacts){
arrTitle.add(contact.title);
}
ArrayList<String> arrId = new ArrayList<>();
for(Contact contact:allContacts){
long i = contact.getId();
String str = Long.toString(i);
arrId.add(str);
}
然後將它們傳送到適配器
mAdapter = new RecyclerAdapter(arrTitle, arrId);
適配器回收站適配器我得到的價值演繹從數據庫中加載進入名單,第二次通過身份證活動
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Convert the id to the array
idTab = new String[mId.size()];
for (int i = 0; i != mId.size(); i++) {
idTab[i] = mId.get(i);
}
// Element position
final int idvadaptere = position;
// Display headers RecyclerView
holder.mTextView.setText(mDataset.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent ddddd = new Intent(context, LastActivity.class);
ddddd.putExtra("id", idTab[idvadaptere]);
context.startActivity(ddddd);
}
});
}
在第二Activiti LastActivity我得到的ID和B這個ID的ASIS在文本字段已經推斷值
// Get the Intent extract from it an object
// Extract from it an object
idString = intent.getStringExtra("id");
// Convert the id in number
idInt = Integer.parseInt(idString);
// Display the text fields
Contact title = Contact.findById(Contact.class, idInt);
String titleStr = title.title;
textView.setText(titleStr);
Contact prich = Contact.findById(Contact.class, idInt);
String prichStr = prich.prich;
textView2.setText(prichStr);
的問題是,如果投入到找一個頭,這個列表的標題位置不會是第五名,例如,與第一和id必須不同於第二個價值aktiviti不暴露第五個id,從第一個。 源搜索,它工作正常,如果你需要複製任何人,唯一的問題是通過單擊列表項傳輸數據。
activity_main.xml中
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search_view"
android:layout_gravity="right"
app:theme="@style/Theme.AppCompat.NoActionBar"
app:searchIcon="@drawable/ic_search_white_24dp"/>
MainActivity.java
private SearchView searchView;
在的onCreate
searchView = (SearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
mAdapter.filter(text);
return false;
}
});
RecyclerAdapter.java
private ArrayList<String> mDataset;
private ArrayList<String> mCleanCopyDataset;
構造
public RecyclerAdapter(ArrayList<String> dataset) {
mDataset = dataset;
mCleanCopyDataset = mDataset;
}
搜索
// The filter() method we iterate through all the items on the list and if any item contains the search text, we add it to a new list mDataset:
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
mDataset = new ArrayList<String>();
if (charText.length() == 0) {
// mCleanCopyDataset we always contains the unaltered and the filter (full) copy of the list of data
mDataset.addAll(mCleanCopyDataset);
} else {
for (String item : mCleanCopyDataset) {
// we iterate through all the items on the list and if any item contains the search text, we add it to a new list mDataset
if (item.toLowerCase(Locale.getDefault()).contains(charText)) {
mDataset.add(item);
}
}
}
// method notifyDataSetChanged() allows you to update the list on the screen after filtration
notifyDataSetChanged();
}
全碼 RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<String> mDataset;
private ArrayList<String> mId;
private ArrayList<String> mCleanCopyDataset;
String[] idTab;
String[] titleTab;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.tv_recycler_item);
}
}
public RecyclerAdapter(ArrayList<String> dataset, String[] titleTab, ArrayList<String> id) {
mDataset = dataset;
titleTab = titleTab;
mId = id;
mCleanCopyDataset = mDataset;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
idTab = new String[mId.size()];
for (int i = 0; i != mId.size(); i++) {
idTab[i] = mId.get(i);
}
holder.mTextView.setText(mDataset.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent ddddd = new Intent(context, LastActivity.class);
ddddd.putExtra("id", idTab[holder.getAdapterPosition()]);
context.startActivity(ddddd);
}
});
}
@Override
public int getItemCount() {
return mDataset.size();
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
mDataset = new ArrayList<String>();
if (charText.length() == 0) {
mDataset.addAll(mCleanCopyDataset);
} else {
for (String item : mCleanCopyDataset) {
if (item.toLowerCase(Locale.getDefault()).contains(charText)) {
mDataset.add(item);
}
}
}
notifyDataSetChanged();
}
}
爲什麼clear();和adapter.add(...);紅? – Artsait
我創建了一個具有這些方法的抽象適配器類。你將不得不直接管理你的數據列表。我將修改答案以適合您的數據集。 – Weava