フィルタリング用のリストビューと編集テキストがあります。結果をフィルタリングして String[] subItems に保存することはできますが、リストビューは更新されません。Google で検索しましたが、解決策が見つかりませんでした。
これがコードスニペットです。
OnActivityCreated
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
lv = getListView();
lv.setTextFilterEnabled(true);
lv.setCacheColorHint(Color.TRANSPARENT);
lv.setFastScrollEnabled(true);
lv.setDivider(getResources().getDrawable(android.R.color.black));
lv.setDividerHeight(1);
//lv.setBackgroundColor(Color.parseColor("#f7f9c3")); //#ffddb0
lv.setBackgroundDrawable(getResources().getDrawable(R.drawable.merge));
lv.setScrollingCacheEnabled(false);
lv.setSmoothScrollbarEnabled(false);
adapter = new IconicAdapter();
setListAdapter(adapter);
}
OnCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.toc, null);
filterText = (EditText) view.findViewById(R.id.editTextFilter);
filterText.addTextChangedListener(filterTextWatcher);
return view;
}
FilterTextWatcher
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
}
};
カスタム ListView クラス
public class IconicAdapter extends ArrayAdapter {
public String[] allItems;
public String[] subItems;
//public ArrayList <String> allItems = new ArrayList<String>();
//public ArrayList <String> subItems = new ArrayList<String>();
private TOCFilter filter;
TextView tx = null;
IconicAdapter() {
super(getActivity(), R.layout.toc_list, Constants.TOC);
this.subItems = new String[Constants.TOC.size()];
this.subItems = Constants.TOC.toArray(this.subItems);
this.allItems = this.subItems;
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new TOCFilter();
}
return filter;
}
@Override
public int getCount() {
//return Constants.TOC.size();
return subItems.length;
}
@Override
public String getItem(int position) {
return subItems[position];
}
public View getView(int position, View convertView,ViewGroup parent) {
//LayoutInflater inflater= getLayoutInflater();
View row = getActivity().getLayoutInflater().inflate(R.layout.toc_list, parent, false);
//View row=inflater.inflate(R.layout.toc_list, parent, false);
try
{
tx=(TextView)row.findViewById(R.id.mainList);
tx.setText(Constants.TOC.get(position).toString());
tx.setTextSize(Constants.txtSize);
tx.setTextColor(Color.BLACK);
if(Constants.UKDebug)
{
if(position == 0 || position == 1)
{
Log.d("iconicadapter", Constants.TOC.get(position) + "");
}
}
}
catch (Exception e) {Log.d("main","iconadapter"); e.printStackTrace();}
return(row);
}
顧客リストビュー アダプターの一部であるフィルター クラス
private class TOCFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<String> i = new ArrayList<String>();
String[] contents;
if (constraint!= null && constraint.toString().length() > 0) {
for (int index = 0; index < allItems.length; index++) {
String si = allItems[index];
if(si.toLowerCase().startsWith(constraint.toString())){
i.add(si);
}
}
contents = new String[i.size()];
i.toArray(contents);
results.values = contents;
results.count = contents.length;
}
else{
synchronized (allItems){
results.values = allItems;
results.count = allItems.length;
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
subItems = (String[]) results.values;
notifyDataSetChanged();
}
}
あなたの助けは非常に高く評価されています。