在android操作栏中搜索自定义列表视图

时间:2016-11-16 10:00:51

标签: android searchview

我有一个名为School.java的活动,它加载了学校的customList。我实现了SearchView来搜索那些学校列表。它显示了以下错误,我点击了操作栏中的搜索字段

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence)' on a null object reference

这是我的school.java,我实现了我的searchView

package com.example.user.educationhunt;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.example.user.educationhunt.adapter.CustomListAdapter;
import com.example.user.educationhunt.adapter.ExampleAdapter;
import com.example.user.educationhunt.pojos.AppController;
import com.example.user.educationhunt.pojos.OurSchool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class School extends AppCompatActivity  implements SearchView.OnQueryTextListener {

    private Menu menu;
    // Log tag

    private static final String TAG = School.class.getSimpleName();

    // Billionaires json url
    private static final String url = "http://myeducationhunt.com/public/schools";

    private ProgressDialog pDialog;
    private List<OurSchool> ourSchoolsListItems = new ArrayList<OurSchool>();
    private ListView listView;
    private CustomListAdapter adapter;
    private SearchView searchView;
    private MenuItem searchMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Schools");


        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, ourSchoolsListItems);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                OurSchool ourSchool = new OurSchool();
                Intent i = new Intent(School.this, SchoolDetails.class);

                i.putExtra("id", ourSchoolsListItems.get(position).schoolId);
                i.putExtra("name", ourSchoolsListItems.get(position).schoolName);
                i.putExtra("location", ourSchoolsListItems.get(position).schoolLocation);
                i.putExtra("logo", ourSchoolsListItems.get(position).schoolLogo);
                i.putExtra("email", ourSchoolsListItems.get(position).schoolEmail);
                i.putExtra("website", ourSchoolsListItems.get(position).schoolWebsite);
                i.putExtra("created_at", ourSchoolsListItems.get(position).createdAt);
                i.putExtra("updated_at", ourSchoolsListItems.get(position).updatedAt);

                startActivity(i);

            }
        });

        pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
        pDialog.setMessage("Loading…");
        pDialog.show();

// Creating volley request obj
        JsonArrayRequest schoolRequest = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                OurSchool ourSchool = new OurSchool();

                                ourSchool.schoolId = obj.getInt("id");
                                ourSchool.schoolName = obj.getString("name");
                                ourSchool.schoolLocation = obj.getString("location");
                                ourSchool.schoolLogo = obj.getString("logo");
                                ourSchool.schoolEmail = obj.getString("email");
                                ourSchool.schoolWebsite = obj.getString("website");
                                ourSchool.createdAt = obj.getString("created_at");
                                ourSchool.updatedAt = obj.getString("updated_at");

                                // adding schools to ourSchool list
                                ourSchoolsListItems.add(ourSchool);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(schoolRequest);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            case R.id.schoolSearch:
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search, menu);

        SearchManager searchManager = (SearchManager)
                getSystemService(Context.SEARCH_SERVICE);
        searchMenuItem = menu.findItem(R.id.schoolSearch);
        searchView = (SearchView) searchMenuItem.getActionView();

        searchView.setSearchableInfo(searchManager.
                getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);

        return true;
    }

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

    @Override
    public boolean onQueryTextChange(String newText) {
        CustomListAdapter.getFilter().filter(newText);

        return true;
    }
}

这是res / menu

中的search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/schoolSearch"
        android:title="Search"
        android:icon="@mipmap/scholsrch"
        app:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

我也在res / xml / searchable

中做了这个
<?xml version="1.0" encoding="utf-8"?>
<searchable
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search">
</searchable>

我终于在清单文件

中定义了我的可搜索配置
<activity
            android:name=".School"
            android:theme="@style/AppTheme1">
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable">
            </meta-data>
        </activity>

任何人都可以帮助我吗?

0 个答案:

没有答案