Привет всем, я создал проект, показанный по ссылке выше. У меня есть SearchView с использованием RecyclerView и CardView, элементы хранятся с использованием ArrayList. У меня есть две проблемы, которые я не могу решить, поэтому, пожалуйста, помогите мне.
Во-первых, SearchView работает, но не совпадает между Name и ColorLike.
Во-вторых, я хочу реализовать несколько фильтров в SearchView, используя Spinner для параметров, чтобы я мог осуществлять поиск по имени или ColorLike. Возможно ли это?
Заранее спасибо.
Вот мой код.
Model.java
package com.example.myapplication;
public class Model {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Adapter.java
package com.example.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Locale;
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private LayoutInflater inflater;
private ArrayList<Model> searchResult;
private ArrayList<Model> arraylist;
public static String[] name;
public static String[] colorlike;
public Adapter(Context context, ArrayList<Model> searchResult) {
inflater = LayoutInflater.from(context);
this.searchResult = searchResult;
this.arraylist = new ArrayList<Model>();
this.arraylist.addAll(MainActivity.searchResult);
}
@Override
public Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.cardview_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tvName.setText(searchResult.get(position).getName());
holder.tvColorLike.setText(colorlike[position]);
}
@Override
public int getItemCount() {
return searchResult.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvColorLike;
private TextView tvName;
MyViewHolder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.item_name);
tvColorLike = (TextView) itemView.findViewById(R.id.item_colorlike);
}
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
MainActivity.searchResult.clear();
if (charText.length() == 0) {
MainActivity.searchResult.addAll(arraylist);
} else {
for (Model model : arraylist) {
if (model.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
MainActivity.searchResult.add(model);
}
}
}
notifyDataSetChanged();
}
}
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.ArrayList;
import static com.example.myapplication.Adapter.colorlike;
import static com.example.myapplication.Adapter.name;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static ArrayList<Model> searchResult;
private Adapter adapter;
private static final String[] paths = {"Filter by Name", "Filter by ColorLike"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
name = getResources().getStringArray(R.array.name);
colorlike = getResources().getStringArray(R.array.colorlike);
searchResult = populateList();
adapter = new Adapter(getApplicationContext(), searchResult);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
Spinner spinner = (Spinner) findViewById(R.id.spinner);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, paths);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter2);
spinner.setOnItemSelectedListener(this);
}
private ArrayList<Model> populateList() {
ArrayList<Model> list = new ArrayList<>();
for (int i = 0; i < 8; i++) {
Model model = new Model();
model.setName(name[i]);
list.add(model);
}
return list;
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return false;
}
});
return true;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
break;
case 1:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="end"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingEnd="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:popupBackground="@color/colorPrimary" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f7f7f7"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="21dp"
android:layout_weight="1"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:layout_weight="1"
android:text="ColorLike"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical" />
</LinearLayout>
cardview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
card_view:cardBackgroundColor="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:text="name"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:id="@+id/item_colorlike"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:text="colorlike"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>