在我的mathod onCreateOptionsMenu中的片段中,该应用程序停止工作

时间:2019-01-22 14:48:40

标签: java android android-fragments filter android-recyclerview

我想通过searchView过滤recycleView。 我将xml文件添加到菜单。 当应用程序运行时,片段打开时她停止了。 当我放置断点时,在一行中发现了问题:SearchView searchView =(SearchView)searchItem.getActionView()-在onCreateOptionsMenu方法中。 为什么会这样? (适配器在片段中创建)

在xml菜单中,我更改为 app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView"/>

@SuppressLint("ValidFragment")
public class AvailableDrivesFragment extends Fragment {
public RecyclerView drivesRecyclerView;
public LinearLayout details;
public List<Drive> drives = new ArrayList<>();
public TextView textDetails;
public Button buttonChoose;
IDataBase fb = FactoryDataBase.getDataBase();
private DrivesRecycleViewAdapter adapter;
private Menu menu;

Driver driver;
@SuppressLint("ValidFragment")
AvailableDrivesFragment(Driver e) {
    this.driver = e;
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_available_drives, container, false);

    textDetails = v.findViewById(R.id.text_details);
    details = v.findViewById(R.id.linear_details);
    details.setVisibility(View.GONE);

    getActivity().setTitle("Available Drives");

    drivesRecyclerView = v.findViewById(R.id.my_list);
    drivesRecyclerView.setHasFixedSize(true);
    drivesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    adapter = new DrivesRecycleViewAdapter();
    drives = fb.getAvailableDrives();
    drivesRecyclerView.setAdapter(adapter);

    setHasOptionsMenu(true);

    return v;
}

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


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();//here stop working
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            try {
                adapter.getFilter().filter(newText);
            }
        catch (Exception r) {
            r.printStackTrace();
        }

            return false;
        }
    });
}


public class DrivesRecycleViewAdapter extends RecyclerView.Adapter<DrivesRecycleViewAdapter.DriveViewHolder> implements Filterable

{
    public List<Drive> drivefull;

    public DrivesRecycleViewAdapter() {
        this.drivefull = new ArrayList<>(drives);
        ;
    }


    @Override
    public DriveViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.item_drive, parent, false);
        return new DriveViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull DriveViewHolder holder, int position) {
        Drive drive = drives.get(position);
        holder.nameTextView.setText(drive.getName());
        holder.nameTextView.setTextSize(20);
        holder.phoneTextView.setText(drive.getStartAddress());
        holder.phoneTextView.setTextSize(16);

    }


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

    @Override
    public Filter getFilter() {

        return filter;
    }

    public Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Drive> filteredList = new ArrayList<>();//new list that contained only filtered items
            if (constraint == null || constraint.length() == 0)//we what to show all the results becuse we don't what filtering
            {
                filteredList.addAll(drivefull);
            } else { //we what to filter the list
                String filterPattern = constraint.toString().toLowerCase().trim(); // sting that takes the input
                for (Drive item : drivefull) {
                    if (item.getName().toLowerCase().contains((filterPattern))) {
                        filteredList.add(item);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;

            return results;
        }


        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            drives.clear();
            drives.addAll((List) results.values);
            notifyDataSetChanged();
        }
    };


    class DriveViewHolder extends RecyclerView.ViewHolder {
        TextView phoneTextView;
        TextView nameTextView;


        public DriveViewHolder(final View itemView) {
            super(itemView);
            phoneTextView = itemView.findViewById(R.id.phone_item_drive);
            nameTextView = itemView.findViewById(R.id.name_item_drive);


            itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

                @Override
                public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                    menu.setHeaderTitle("Select Action");
                    MenuItem detailm = menu.add(Menu.NONE, 1, 1, "view details");
                    detailm.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {

                            Drive drive = drives.get(getAdapterPosition());
                            textDetails.setText(drive.toString());
                            details.setVisibility(View.VISIBLE);


                            return true;
                        }
                    });
                    MenuItem addDrive = menu.add(Menu.NONE, 1, 1, "take drive");
                    addDrive.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            final Drive drive = drives.get(getAdapterPosition());

                            fb.changeStatus(drive.getId(), driver, DriveStatus.TREATMENT, new IDataBase.Action() {
                                @Override
                                public void onSuccess() {

                                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                    builder.setMessage("The drive is in your care!")
                                            .setCancelable(false)
                                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int id) {
                                                    String mail = drive.getEmail();
                                                    String[] mails = mail.split(",");
                                                    Intent in = new Intent(Intent.ACTION_SEND);
                                                    in.putExtra(Intent.EXTRA_EMAIL, mails);
                                                    in.putExtra(Intent.EXTRA_SUBJECT, "get taxi");
                                                    in.putExtra(Intent.EXTRA_TEXT, "taxi will coming to you in few minutes");
                                                    in.setType("message/rfc822");
                                                    startActivity(Intent.createChooser(in, "choose email"));
                                                }
                                            });
                                    AlertDialog alert = builder.create();
                                    alert.show();


                                    drives.remove(getAdapterPosition());


                                    drivesRecyclerView.getAdapter().notifyDataSetChanged();
                                }

                                @Override
                                public void onFailure(Exception exception) {
                                    Toast.makeText(getActivity(), "הלקיחה נכשלה", Toast.LENGTH_LONG).show();

                                }

                                @Override
                                public void onProgress(String status, double percent) {
                                    ;
                                }
                            });


                            return true;
                        }
                    });


                }
            });
        }
    }
}

}

0 个答案:

没有答案