我创建了一个SearchActivity
并将它导入到ArrayList
中,如下所示:
在我的MainActivity
中:
private ArrayList<Accordo> getChords() {
ArrayList<Accordo> chords = new ArrayList<>();
Accordo a=new Accordo();
a.setName("Do maggiore");
a.setNote("Do, Mi, Sol");
a.setImage(R.drawable.do_maggiore);
chords.add(a);
a=new Accordo();
a.setName("do 5");
a.setNote("na, na, na");
a.setImage(R.drawable.do5);
chords.add(a);
a=new Accordo();
a.setName("do 6");
a.setNote("na,na,na");
a.setImage(R.drawable.do6);
chords.add(a);
//other elements
return chords;
}
/** start SearchActivity when ImageButton is pressed */
ImageButton cerca = (ImageButton) findViewById(R.id.search);
cerca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
ArrayList<Accordo> chords = new ArrayList<Accordo>();
intent.putExtra("chords", chords);
startActivity(intent);
}
});
这是我的
SearchActivity
:public class SearchActivity extends Activity {
SearchView sv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
ArrayList<Accordo> chords = (ArrayList<Accordo>) getIntent().getSerializableExtra("chords");
sv = (SearchView) findViewById(R.id.testo_ricerca);
RecyclerView rv = (RecyclerView) findViewById(R.id.lista_ricerca);
//SET THE PROPERTIES
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setItemAnimator(new DefaultItemAnimator());
//SET ADAPTER
final SearchAdapter adapter = new SearchAdapter(this, chords);
rv.setAdapter(adapter);
//SEARCH
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//FILTER AS YOU TYPE
adapter.getFilter().filter(newText);
return false;
}
});
}
}
现在,即使我已经创建了一个自定义适配器,我的
RecyclerView
中的SearchActivity
也不显示任何项目。这是我的适配器:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchHolder> implements Filterable {
Context context;
ArrayList<Accordo> chords, filterList;
SearchFilter filter;
public class SearchHolder extends RecyclerView.ViewHolder {
//VIEWS
TextView nome;
public SearchHolder(View view){
super(view);
this.nome = (TextView) view.findViewById(R.id.ricerca_nome);
}
}
public SearchAdapter(Context ctx, ArrayList<Accordo> chords){
this.context = ctx;
this.chords = chords;
this.filterList = chords;
}
@Override
public SearchHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// convert xml to View Object
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_row, parent, false);
return new SearchHolder(v);
}
@Override
public void onBindViewHolder(SearchHolder holder, int position) {
//BIND DATA
holder.nome.setText(chords.get(position).getNome());
}
@Override
public int getItemCount() {
return chords.size();
}
@Override
public Filter getFilter() {
if (filter==null)
filter = new SearchFilter(filterList, this); //this e' l'adapter stesso
return filter;
}
}
当我尝试构建应用程序时,我没有收到任何错误,因此我真的不知道为什么会发生这种情况。
从上面的代码中可以看到,我为
search_row
自定义了searchRecyclerView
,但是仍然应该以正确的方式设置适配器。希望你能帮助我!
编辑:
这是我的SearchFilter.class
public class SearchFilter extends Filter{
SearchAdapter adapter;
ArrayList<Accordo> filterList;
public SearchFilter(ArrayList<Accordo> filterList, SearchAdapter adapter) {
this.adapter=adapter;
this.filterList=filterList;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
//CHECK CONSTRAINT VALIDITY
if(constraint!=null && constraint.length()>0){
/**CHANGE TO UPPER*/
constraint=constraint.toString().toUpperCase();
/**STORE THE FILTERED CHORDS*/
ArrayList<Accordo> filteredChords = new ArrayList<>();
for(int i=0; i<filterList.size();i++){
//CHECK
if(filterList.get(i).getNome().toUpperCase().contains(constraint))
//ADD CHORD TO FILTEREDCHPRDS
filteredChords.add(filterList.get(i));
}
results.count = filteredChords.size();
results.values = filteredChords;
} else {
results.count = filterList.size(); //0
results.values = filterList; //null
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.filterList = (ArrayList<Accordo>) results.values;
//REFRESH
adapter.notifyDataSetChanged();
}
}
最佳答案
请检查我编辑的代码:
请按如下方式编辑您的Accordo课程:
public class Accordo implements Serializable {
int image;
String name;
String note;
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Accordo(int image, String name, String note) {
this.image = image;
this.name = name;
this.note=note;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后如下编辑您的Activity类:
public class YOUR_SENDING_ACTIVITY extends Activity{
ArrayList<Accordo> chords =null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOURLAYOUT);
chords = new ArrayList<Accordo>();
chords.add(new Accordo(R.drawable.do_maggiore,"Do maggiore","Do, Mi, Sol"));
chords.add(new Accordo(R.drawable.do5,"Do 5","Na, na, na"))
ImageButton cerca = (ImageButton) findViewById(R.id.search);
cerca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
intent.putExtra("chords", chords);
startActivity(intent);
}
});
}
}
将您的SearchActivity替换为以下内容:
public class SearchActivity extends AppCompatActivity {
RecyclerView rv;
SearchView sv;
ArrayList<Accordo> chords = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
chords = (ArrayList<Accordo>) getIntent().getSerializableExtra("chords");
sv = (SearchView) findViewById(R.id.testo_ricerca);
rv = (RecyclerView) findViewById(R.id.lista_ricerca);
rv.setLayoutManager(new LinearLayoutManager(SearchActivity.this, LinearLayoutManager.VERTICAL, false));
rv.setHasFixedSize(true);
final SearchAdapter adapter = new SearchAdapter(this, chords);
rv.setAdapter(adapter);
//SEARCH
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//FILTER AS YOU TYPE
final List<Accordo> filteredModelList = filter(chords, newText);
adapter.setFilter(filteredModelList);
return true;
}
});
}
private List<Accordo> filter(List<Accordo> models, String query) {
query = query.toLowerCase();
final List<Accordo> filteredModelList = new ArrayList<>();
for (Accordo model : models) {
final String text = model.getName().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
}
和具有以下类的SearchAdapter:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchHolder>{
Context context;
ArrayList<Accordo> chords;
public class SearchHolder extends RecyclerView.ViewHolder {
//VIEWS
TextView nome;
public SearchHolder(View view){
super(view);
this.nome = (TextView) view.findViewById(R.id.ricerca_nome);
}
}
public SearchAdapter(Context ctx, ArrayList<Accordo> chords){
this.context = ctx;
this.chords = chords;
// this.filterList = chords;
}
@Override
public SearchHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// convert xml to View Object
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_row, parent, false);
return new SearchHolder(v);
}
@Override
public void onBindViewHolder(SearchHolder holder, int position) {
//BIND DATA
holder.nome.setText(chords.get(position).getName());
}
@Override
public int getItemCount() {
return chords.size();
}
public void setFilter(List<Accordo> accordoList) {
chords = new ArrayList<>();
chords.addAll(accordoList);
notifyDataSetChanged();
}
}