2015-08-18 50 views
-1

我用卡lib庫和添加在同一個活動一個搜索欄,當我搜索一個特定的卡,但卡上沒有我的搜索文本過濾創造了一個Android的應用程序.. 我被採取參考https://github.com/gabrielemariotti/cardslib卡過濾列表中的Android

在主要活動

..(上創建方法)

創建卡

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

}

回答

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;