我使用卡片库创建了一个Android应用程序,并在搜索特定卡片时在同一活动中添加搜索栏,但卡片未按我的搜索文本进行过滤。 我正在https://github.com/gabrielemariotti/cardslib
参考 主要活动中的..(在创建方法上)
创建卡
medicine_title=getResources().getStringArray(R.array.medicine_title);
final ArrayList<Card> cards = new ArrayList<Card>();
//Search View Find by Layout
searchview = (SearchView)findViewById(R.id.searchView);
for (String loop:medicine_title) {
// Create a Card
card = new Card(this,R.layout.row_card);
// Create a CardHeader
CardHeader header = new CardHeader(this);
// Add Header to card
header.setTitle(loop);
card.addCardHeader(header);
card.setTitle("0");
/* CardThumbnail thumb = new CardThumbnail(this);
thumb.setDrawableResource(listImages[i]);
card.addCardThumbnail(thumb);
*/
cards.add(card);
card.setOnClickListener(new Card.OnCardClickListener() {
@Override
public void onClick(final Card card, View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(con)
.setTitle("Quantity");
final EditText input = new EditText(con);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text s
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
quantitytemp = input.getText().toString();
card.setTitle(quantitytemp);
}
});
///work done
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.show();
}
});
}
final CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this, cards);
CardListView listView = (CardListView) this.findViewById(R.id.myList);
if (listView != null) {
listView.setAdapter(mCardArrayAdapter);
}
searchView方法
searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
MyFilter myFilter = new MyFilter(MainActivity.this,cards,mCardArrayAdapter);
myFilter.getFilter().filter(s);
return true;
}
});
}
这是我的自定义过滤器代码
public class MyFilter extends CardArrayAdapter implements Filterable {
Context context;
List<Card> cards;
CardArrayAdapter mcardCardArrayAdapter;
public MyFilter(Context context, List<Card> cards,CardArrayAdapter mcardCardArrayAdapter) {
super(context, cards);
this.context = context;
this.cards = cards;
this.mcardCardArrayAdapter = mcardCardArrayAdapter;
}
@Override
public Filter getFilter() {
Filter cardFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<Card> tempList = new ArrayList<Card>();
// Where constraint is the value you're filtering against and
// cards is the original list of elements
if(constraint != null ) {
// Iterate over the cards list and add the wanted
// items to the tempList
filterResults.values = tempList;
filterResults.count = tempList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// Update your adapter here and notify
mcardCardArrayAdapter.addAll((Card[]) results.values);
mcardCardArrayAdapter.notifyDataSetChanged();
}
};
return cardFilter;
}
}
答案 0 :(得分:0)
您粘贴的代码中有注释表明您的代码究竟出了什么问题。而不是
的行//迭代卡片列表并添加想要的
//项目到tempList
你应该迭代卡片列表并将想要的项目添加到tempList中,如下所示:
final String filterPattern = constraint.toString().toLowerCase();
for (final Card card : cards) {
if (card.getTitle().toLowerCase().contains(filterPattern))
tempList.add(card);
}
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;