如何在MySQL上过滤RecyclerView

时间:2015-12-06 17:32:12

标签: mysql filter android-recyclerview searchview

我尝试使用Cards在RecyclerView上添加搜索过滤器。 我看一下这个教程。

How to filter a RecyclerView with a SearchView

但是无法工作......

这是我的编码:

MainFragment.java

public class MainFragment extends Fragment implements SearchView.OnQueryTextListener {

public static MainFragment newInstance() {
    return new MainFragment();
}

private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private List<ExampleModel> mModels;
private Config config;

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

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    mModels = new ArrayList<>();

    mAdapter = new ExampleAdapter(Config.ids,Config.titles);
    mRecyclerView.setAdapter(mAdapter);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_main, menu);

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

@Override
public boolean onQueryTextChange(String query) {
    final List<ExampleModel> filteredModelList = filter(mModels, query);
    mAdapter.animateTo(filteredModelList);
    mRecyclerView.scrollToPosition(0);
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

private List<ExampleModel> filter(List<ExampleModel> models, String query) {
    query = query.toLowerCase();

    final List<ExampleModel> filteredModelList = new ArrayList<>();
    for (ExampleModel model : models) {
        final String text = model.getTitle().toLowerCase();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }
    return filteredModelList;
}}

ExampleAdapter.java

public class ExampleAdapter extends RecyclerView.Adapter<ExampleViewHolder> {
private final List<ExampleModel> mModels;

public ExampleAdapter(String[] ids, String[] titles, Bitmap[] images) {

    mModels = new ArrayList<ExampleModel>();
    for(int i =0; i<titles.length; i++){
        ExampleModel item = new ExampleModel();
        item.setId("ID :"+ids[i]);
        item.setTitle("標題 :" + titles[i]);
        item.setImage(images[i]);
        mModels.add(item);
    }
}

@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_example, parent, false);
    return new ExampleViewHolder(itemView);
}

@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
    ExampleModel model = mModels.get(position);
    holder.textViewId.setText(model.getId());
    holder.textViewTitle.setText(model.getTitle());
    holder.imageView.setImageBitmap(model.getImage());
}

@Override
public int getItemCount() {
    return mModels.size();
}

public void animateTo(List<ExampleModel> models) {
    applyAndAnimateRemovals(models);
    applyAndAnimateAdditions(models);
    applyAndAnimateMovedItems(models);
}

private void applyAndAnimateRemovals(List<ExampleModel> newModels) {
    for (int i = mModels.size() - 1; i >= 0; i--) {
        final ExampleModel model = mModels.get(i);
        if (!newModels.contains(model)) {
            removeItem(i);
        }
    }
}

private void applyAndAnimateAdditions(List<ExampleModel> newModels) {
    for (int i = 0, count = newModels.size(); i < count; i++) {
        final ExampleModel model = newModels.get(i);
        if (!mModels.contains(model)) {
            addItem(i, model);
        }
    }
}

private void applyAndAnimateMovedItems(List<ExampleModel> newModels) {
    for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
        final ExampleModel model = newModels.get(toPosition);
        final int fromPosition = mModels.indexOf(model);
        if (fromPosition >= 0 && fromPosition != toPosition) {
            moveItem(fromPosition, toPosition);
        }
    }
}

public ExampleModel removeItem(int position) {
    final ExampleModel model = mModels.remove(position);
    notifyItemRemoved(position);
    return model;
}

public void addItem(int position, ExampleModel model) {
    mModels.add(position, model);
    notifyItemInserted(position);
}

public void moveItem(int fromPosition, int toPosition) {
    final ExampleModel model = mModels.remove(fromPosition);
    mModels.add(toPosition, model);
    notifyItemMoved(fromPosition, toPosition);
}}

这是我的logcat:

Caused by: java.lang.NullPointerException: Attempt to get length of null array
        at com.github.wrdlbrnft.searchablerecyclerviewdemo.ui.adapter.ExampleAdapter.<init>(ExampleAdapter.java:23)
        at com.github.wrdlbrnft.searchablerecyclerviewdemo.ui.fragments.MainFragment.onViewCreated(MainFragment.java:62)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
        at android.app.Activity.performStart(Activity.java:6006)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)

0 个答案:

没有答案