简体   繁体   English

为什么在顶部栏中的搜索在 android studio 中不起作用?

[英]Why is search in top bar not working in android studio?

I made an app with listView and I want to use search in top bar but it doesn't work.我用listView制作了一个应用程序,我想在顶部栏中使用搜索,但它不起作用。 In this program, I get the data from the firebase.在这个程序中,我从 firebase 获取数据。

These are the codes below:这些是下面的代码:

This is MainActivity code:这是MainActivity代码:

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int List_APP_LOADER = 0;

    ArrayList<Informatin> listItem;
    ListView listView;
    listAdapter adapter;

    DatabaseReference reference;

    ArrayList<Informatin> mInformation;

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

        listItem = new ArrayList<>();
        listView = findViewById(R.id.list);

        LoaderManager.getInstance(this).initLoader(LIST_APP_LOADER, null, this);

        mInformation = new ArrayList<>();

        adapter = new listAdapter(this, mInformation);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_main_screen, menu);

        MenuItem searchItem = menu.findItem(R.id.search_item);
        SearchView searchView = (SearchView) searchItem.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                listView.getFilter().filter(newText);
                return true;
            }
        });
        return true;
    }

}

This is listAdapter code这是listAdapter代码

public class listAdapter extends ArrayAdapter<Informatin> implements Filterable {

    List<Informatin> arrayList;

    Context context;
    public listAdapter(Context context, ArrayList<Informatin> informatins) {
        super(context, 0, informatins);
        this.context=context;
        arrayList = informatins;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        Informatin currentList = getItem(position);

        TextView nameTextView = listItemView.findViewById(R.id.name);
        TextView descriptionTextView = listItemView.findViewById(R.id.description);
        TextView ageTextView = listItemView.findViewById(R.id.age);
        ImageView imageView = listItemView.findViewById(R.id.image);

        String firstName = currentList.getFirstName();
        String lastName = currentList.getLastName();

        String name = firstName + " " + lastName;

        nameTextView.setText(name);
        descriptionTextView.setText(currentList.getDescription());

        ageTextView.setText(currentList.getAge());

        return listItemView;
    }

    List<Informatin> informatins;
    public Filter getFilter() {
        return new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults oReturn = new FilterResults();
                final ArrayList<Informatin> results = new ArrayList<>();
                if (informatins == null)
                    informatins = arrayList;
                if (constraint != null) {
                    if (informatins != null && informatins.size() > 0) {
                        for (final Informatin g : informatins) {
                            if (g.getFirstName().toLowerCase()
                                    .contains(constraint.toString()))
                                results.add(g);
                        }
                    }
                    oReturn.values = results;
                }
                return oReturn;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                arrayList = (ArrayList<Informatin>) results.values;
                notifyDataSetChanged();
            }
        };
    }
}

this is the picture of the problem这是问题的图片

this is a screenshot for the problem from the device这是设备问题的屏幕截图

Add this to your listadapter code:将此添加到您的 listadapter 代码中:

    ArrayList<Informatin> informatins;
    public Filter getFilter() {
    return new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            final FilterResults oReturn = new FilterResults();
            final ArrayList<Informatin> results = new ArrayList<Informatin>();
            if (informatins == null)
                informatins = employeeArrayList;
            if (constraint != null) {
                if (informatins != null && informatins.size() > 0) {
                    for (final Information g : informatins) {
                        if (g.getName().toLowerCase()
                                .contains(constraint.toString()))
                            results.add(g);
                    }
                }
                oReturn.values = results;
            }
            return oReturn;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            arraylist = (ArrayList<Informatin>) results.values;
            notifyDataSetChanged();
        }
    };
}

Add this in your onquerytextchaged:将此添加到您的 onquerytextchaged 中:

       yourlistView.getFilter().filter(newText)

In my opinion you don't need the Filterable interface at all with the implementation you are doing here.在我看来,您在这里所做的实现根本不需要Filterable接口。 To fix the problem in your code you can do the following要解决代码中的问题,您可以执行以下操作

public class listAdapter extends ArrayAdapter<Informatin> {


public listAdapter(Context context, List<Informatin> informatins) {
    super(context, 0, informatins);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }

    Informatin currentList = getItem(position);

    TextView nameTextView = listItemView.findViewById(R.id.name);
    TextView descriptionTextView = listItemView.findViewById(R.id.description);
    TextView ageTextView = listItemView.findViewById(R.id.age);
    ImageView imageView = listItemView.findViewById(R.id.image);

    String firstName = currentList.getFirstName();
    String lastName = currentList.getLastName();

    String name = firstName + " " + lastName;

    nameTextView.setText(name);
    descriptionTextView.setText(currentList.getDescription());

    ageTextView.setText(currentList.getAge());

    return listItemView;
}

Search code搜索代码

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            newText = newText.toLowerCase();
            ArrayList<Informatin> newList = new ArrayList<>();
            for (Informatin informatin : listItem) {
                String firstName = informatin.getFirstName().toLowerCase();
                if (firstName.contains(newText)) {
                    newList.add(informatin);
                }
            }
            adapter.clear();
            adapter.addAll(newList);
            adapter.notifyDataSetChanged();
            return true;
        }
    });
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM