-1

我使用卡片库库在 android 中创建了一个应用程序,并在我搜索特定卡片时在同一活动中添加了一个搜索栏,但卡片没有按我的搜索文本过滤。我正在参考https://github.com/gabrielemariotti /卡片库

在主要活动中..(在创建方法上)

创建卡片

    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;
}

}

4

1 回答 1

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;
于 2015-08-18T13:27:57.387 回答