我正在尝试实施searchview。它以某种方式工作,但问题是,在你开始输入后,它是单向的。你得到的信息,但如果删除文本,你将无法得到开头的结果。我正在使用cardviews。 标记是模型(gettitle等)
我的适配器:
public class AdapterRestavracije extends
RecyclerView.Adapter<AdapterRestavracije.ViewHolderRestavracije> {
ArrayList<Marker> arrayList;
private LayoutInflater layoutInflater;
private VolleySingleton mVolleySingleton;
Context context;
public AdapterRestavracije(Context context, ArrayList<Marker> models) {
layoutInflater = LayoutInflater.from(context);
this.context = context;
mVolleySingleton = VolleySingleton.getsIstance();
arrayList = new ArrayList<Marker>(models);
// this.arrayList = new ArrayList<Marker>();
}
public void setRestaurants(ArrayList<Marker> arrayList) {
this.arrayList = arrayList;
notifyItemChanged(0, arrayList.size());
}
@Override
public ViewHolderRestavracije onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.restaurants_rview, parent, false);
ViewHolderRestavracije viewHolder = new ViewHolderRestavracije(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolderRestavracije holder, int position) {
Marker currentRest = arrayList.get(position);
holder.name.setText(currentRest.getTitle());
holder.address.setText(currentRest.getAddress());
holder.placilo.setText(currentRest.getValue_of_charge() + " €");
holder.oddaljenost.setText(currentRest.getOddaljenost());
holder.idrestavracije.setText(currentRest.getId());
try {
holder.ratingBarOverall.setRating(Float.parseFloat(currentRest.getOverallRating()));
} catch (Exception e) {
holder.ratingBarOverall.setRating(0);
}
Log.d("RESTAVRACIJEEEEE", currentRest.getTitle());
}
@Override
public int getItemCount() {
return arrayList.size();
}
class ViewHolderRestavracije extends RecyclerView.ViewHolder {
private TextView name, oddaljenost, address, textPlacilo, placilo, idrestavracije;
private RatingBar ratingBarOverall;
private int id;
public View view;
public ViewHolderRestavracije(View itemView) {
super(itemView);
view = itemView;
name = (TextView) itemView.findViewById(R.id.tvNameRestaurant);
oddaljenost = (TextView) itemView.findViewById(R.id.tvOddaljenost);
address = (TextView) itemView.findViewById(R.id.tvAddress);
textPlacilo = (TextView) itemView.findViewById(R.id.tvDoplacilo);
placilo = (TextView) itemView.findViewById(R.id.tvDoplaciloEvri);
idrestavracije = (TextView) itemView.findViewById(R.id.idrestavracije);
ratingBarOverall = (RatingBar) itemView.findViewById(R.id.ratingOverallRestavracije);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//id = getAdapterPosition();
//Log.d("CLICKED", idrestavracije.getText().toString() + "");
//Log.d("CLICKED", name.getText().toString() + "");
String idrest = idrestavracije.getText().toString();
// Log.d("CLICKED", intid + "");
// Intent i = new Intent(context, RestaurantActivity.class);
// i.putExtra("ID_rest", intid);
// context.startActivity(i);
Intent i = new Intent(context, RestaurantActivity.class);
Bundle bundle = new Bundle();
bundle.putString("ID_rest", idrest);
i.putExtras(bundle);
context.startActivity(i);
}
});
}
}
////PRIPRAVA ZA SEARCH//////
public void animateTo(ArrayList<Marker> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
}
private void applyAndAnimateRemovals(ArrayList<Marker> newModels) {
for (int i = arrayList.size() - 1; i >= 0; i--) {
final Marker model = arrayList.get(i);
if (!newModels.contains(model)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(ArrayList<Marker> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final Marker model = newModels.get(i);
if (!arrayList.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(ArrayList<Marker> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final Marker model = newModels.get(toPosition);
final int fromPosition = arrayList.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public Marker removeItem(int position) {
final Marker model = arrayList.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, Marker model) {
arrayList.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final Marker model = arrayList.remove(fromPosition);
arrayList.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
////PRIPRAVA ZA SEARCH//////
}
MY FRAGMENT:
---OTHER CODE----
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
recyclerView = (RecyclerView) view.findViewById(R.id.listRestaurants);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// recyclerView.setItemAnimator(new DefaultItemAnimator());
listRestaurants = new ArrayList<Marker>();
jsonRequest(latitude, longitude);
adapterRestavracije = new AdapterRestavracije(getActivity(),listRestaurants);
recyclerView.setAdapter(adapterRestavracije);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_menu2, menu);
MenuItem item = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(item);
sv.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onQueryTextSubmit(String query) {
// Utils.LogDebug("Submitted: "+query);
Log.d("Clicked: ", query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
// Utils.LogDebug("Changed: " + newText);
final ArrayList<Marker> filteredModelList = filter(listRestaurants, query);
adapterRestavracije.animateTo(filteredModelList);
recyclerView.scrollToPosition(0);
//Log.d("Query Changed: ", query);
return false;
}
private ArrayList<Marker> filter(ArrayList<Marker> listRestaurants, String query) {
query = query.toLowerCase();
final ArrayList<Marker> filteredModelList = new ArrayList<Marker>();
for (Marker model : listRestaurants) {
final String text = model.getTitle().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
---OTHER CODE----
感谢您的帮助。
答案 0 :(得分:0)
示例:
之前:
List<ExampleModel> mModels;
后:
private final List<ExampleModel> mModels;