リストビューにRSSから取得したデータを表示するアンドロイドアプリを作っています。
titleとdescriptionに検索文字を含むリストだけを1文字入力するたびに
フィルタリングしていきたいのですが、フィルタリングがうまくいきません。
(下記のコードはとりあえずタイトルだけ比較してます。)
CustomAdapter.javaのgetFilterメソッド内のコメントアウトされているclear()を実行すると
元のobjects(リストデータ)が全部消えてしまって、検索文字を削除したときや2文字目以降の
入力したときの動きがうまくないです。(リストが復帰しない)
どうすればよろしいでしょうか?
CustomAdapter.java
java
1public class CustomAdapter extends ArrayAdapter<RssLine> { 2 private LayoutInflater layoutInflater_; 3 private List<RssData> objects; 4 private List<RssData> mDisplayedValues; 5 private Context context; 6 private int layoutId; 7 8 public CustomAdapter(Context context, int textViewResourceId, List<RssData> objects) { 9 super(context, textViewResourceId, objects); 10 layoutInflater_ = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 11 12 this.objects = objects; 13 this.context = context; 14 layoutId = textViewResourceId; 15 } 16 @Override 17 public View getView(int position, View convertView, ViewGroup parent) { 18 //省略 19 } 20 21 @Override 22 public Filter getFilter() { 23 Filter filter = new Filter() { 24 25 @SuppressWarnings("unchecked") 26 @Override 27 protected void publishResults(CharSequence constraint,FilterResults results) { 28 if(results.count > 0) { 29 mDisplayedValues = (ArrayList<RssData>) results.values; // has the filtered values 30// clear(); 31 addAll(mDisplayedValues); 32 notifyDataSetChanged(); // notifies the data with new filtered values 33 } 34 else { 35 notifyDataSetInvalidated(); 36 } 37 } 38 39 @Override 40 protected FilterResults performFiltering(CharSequence constraint) { 41 FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values 42 ArrayList<RssData> FilteredArrList = new ArrayList<RssData>(); 43 44 if (objects == null) { 45 objects = new ArrayList<RssLine>(mDisplayedValues); // saves the original data in mOriginalValues 46 } 47 48 /******** 49 * 50 * If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values 51 * else does the Filtering and returns FilteredArrList(Filtered) 52 * 53 ********/ 54 if (constraint == null || constraint.length() == 0) { 55 56 // set the Original result to return 57 results.count = objects.size(); 58 results.values = objects; 59 } 60 else { 61 constraint = constraint.toString().toLowerCase(); 62 for (int i = 0; i < objects.size(); i++) { 63 RssData item = getItem(i); 64 String data = objects.get(i).getTitle(); 65 if((data != null && data.contains(constraint))) { 66 FilteredArrList.add(item); 67 } 68 } 69 // set the Filtered result to return 70 results.count = FilteredArrList.size(); 71 results.values = FilteredArrList; 72 } 73 return results; 74 } 75 }; 76 return filter; 77 } 78}
MainActivity.java
java
1 public CustomAdapter adapter; 2 @SuppressWarnings("unchecked") 3 @Override 4 public boolean onQueryTextChange(String newText) { 5 // 文字が入力されるたびにフィルタを更新します 6 adapter.getFilter().filter(newText); 7 return true; 8 }
以下はリストに持たせるクラスです。
RssData.java
java
1public class RssData { 2 private Bitmap imageData; 3 private String title; 4 private String description; 5 6 public void setImagaData(Bitmap image) { 7 imageData = image; 8 } 9 10 public Bitmap getImageData() { 11 return imageData; 12 } 13 14 public void setTitle(String text) { 15 title = text; 16 } 17 18 public String getTitle() { 19 return title; 20 } 21 22 public void setDescription(String text) { 23 description = text; 24 } 25 26 public String getDescription() { 27 return description; 28 } 29}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/16 11:30
2016/02/16 13:44
2016/02/16 15:19
2016/02/17 00:26 編集
2016/02/17 13:24
2016/02/17 13:37