如何在CursorAdapter中实现Filterable

时间:2015-10-01 07:21:49

标签: android android-cursoradapter

我想在我的适配器中实现自定义Filterable。怎么实现呢?

ScrollCursorAdapter.java

public class ScrollCursorAdapter extends CursorAdapter implements Filterable{
private int count;
private int stepNumber;
private int startCount;
private Cursor myCursor;
private Cursor oriCursor;
Context context;

public ScrollCursorAdapter(Context context, Cursor c, int flags, int startCount, int stepNumber) {
    super(context, c, flags);
    this.startCount = Math.min(startCount, c.getCount()); //don't try to show more views than we have
    this.count = this.startCount;
    this.myCursor = c;
    this.oriCursor = c;
    this.stepNumber = stepNumber;
    this.context =context;

}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater inflater = LayoutInflater.from(context);

    return inflater.inflate(R.layout.list, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView textWords = (TextView) view.findViewById(R.id.tvList);
    textWords.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.RUNG)));
}


@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    return new Filter(){

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            final FilterResults filterReturn = new FilterResults();
            Cursor c = null;
            if(constraint != null){
                c = getFilterQueryProvider().runQuery(constraint);
            }
            filterReturn.values = c;
            return filterReturn;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            myCursor = (Cursor) results.values;
            notifyDataSetChanged();
        }

    };
}

}

LookupFragment.java

public class LookupFragment extends Fragment implements OnQueryTextListener, FilterQueryProvider/*OnScrollListener, OnItemClickListener, */{

private ListView listView;
private Handler handler;
private ScrollCursorAdapter adapter;
private DatabaseHelper dbHelper;
private ProgressBar progressBar;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_lookup, container, false);

    setHasOptionsMenu(true);

    handler = new Handler();
    dbHelper = new DatabaseHelper(getContext());
    listView = (ListView) view.findViewById(R.id.lookup_list);

    progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyle);
    //progressBar.setVisibility((20 < rowItems.size())? View.VISIBLE : View.GONE);
    progressBar.getIndeterminateDrawable().setColorFilter(getColorWrapper(getContext(), R.color.light_green), 
            android.graphics.PorterDuff.Mode.MULTIPLY);

    dbHelper.open();
    Cursor cursor = dbHelper.getRung();

    adapter = new ScrollCursorAdapter(getContext(), cursor, 0, 20, 10);
    adapter.setFilterQueryProvider(this);

    listView.setAdapter(adapter);
    listView.addFooterView(progressBar);

    return view;
}

@SuppressWarnings("deprecation")
public static int getColorWrapper(Context context, int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.getColor(id);
    } else {
        return context.getResources().getColor(id);
    }
}



@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

     SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
     searchView.setOnQueryTextListener(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onQueryTextChange(String str) {
    adapter.getFilter().filter(str.toString());
    return true;
}

@Override
public boolean onQueryTextSubmit(String arg0) {
    // TODO Auto-generated method stub
    return false;
}

//implements FilterQueryProvider
@Override
public Cursor runQuery(CharSequence constraint) {
    return dbHelper.getWordsByQuery(constraint.toString());
    }
}

0 个答案:

没有答案