【问题标题】:Card List Filtering in AndroidAndroid中的卡片列表过滤
【发布时间】:2015-08-18 12:43:51
【问题描述】:

我使用卡片库库在 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.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;
}

}

【问题讨论】:

    标签: android filtering cardslib


    【解决方案1】:

    您粘贴的代码中有一些 cmets 可以准确指出您的代码出了什么问题。而不是带有

    的行

    // 遍历卡片列表并添加想要的卡片

    // 临时列表中的项目

    你应该遍历卡片列表并将想要的项目添加到 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;
    

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多