如何使搜索栏与我的列表视图一起使用

时间:2018-12-03 01:22:08

标签: android-studio

我一直在努力在视图中创建搜索栏,以通过列表视图中的服务标题查找任何服务。我进行了一些更改,但是到目前为止,没有任何功能可以正常工作。 我在活动filter中创建了方法ServiceAdapter.java,为此创建了一个名为filterText的editText

ServiceListActivity.java

filterText = (EditText)findViewById(R.id.filterText);
mServicesListView = (ListView)findViewById(R.id.service_list_view);

mServiceAdapter = new ServiceAdapter(ServiceListActivity.this, mServices);
//mServiceAdapter.notifyDataSetChanged();
//mServicesListView.setAdapter(mServiceAdapter);

filterText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //ServiceListActivity.this.mServiceAdapter.filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            String text = filterText.getText().toString().toLowerCase(Locale.getDefault());
            mServiceAdapter.filter(text);
        }
});

ServiceAdapter.java

public class ServiceAdapter extends ArrayAdapter<Service> {

    private final List<Service> mServiceList;
    private List<Service> tmp;

    private DatabaseReference mDatabaseServices;
    private DatabaseReference mDatabaseUsers;
    private FirebaseAuth mAuth;

    private String mUserId;
    private int mUserPoints;

    public ServiceAdapter(Context context, List<Service> serviceList) {
        super(context, R.layout.service_list_item_view, serviceList);
        mServiceList = serviceList;
        this.tmp = serviceList;
    }


    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView = LayoutInflater.from(getContext()).inflate(R.layout.service_list_item_view, parent, false);

        final Service service = mServiceList.get(position);

        mDatabaseServices = FirebaseDatabase.getInstance().getReference("services");
        mDatabaseUsers = FirebaseDatabase.getInstance().getReference("users");
        mAuth = FirebaseAuth.getInstance();
        mUserId = mAuth.getCurrentUser().getUid();

        ImageView serviceIcon = rowView.findViewById(R.id.service_icon);
        TextView serviceTitle = rowView.findViewById(R.id.service_title);
        TextView serviceResource = rowView.findViewById(R.id.service_resource);

        serviceIcon.setImageResource(R.drawable.service_icon);
        serviceTitle.setText(service.getTitle());
        serviceResource.setText(service.getResource());

        mDatabaseUsers.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    User user = snapshot.getValue(User.class);
                    if (user.getId().equals(mUserId))
                        mUserPoints = user.getPoints();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        return rowView;
    }

    //Filter class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        mServiceList.clear();
        if (charText.length() == 0) {
            mServiceList.addAll(tmp);
        } else {
           for (Service s: tmp) {
               if(s.getTitle().toLowerCase(Locale.getDefault()).contains(charText)) {
                    System.out.println(s.getTitle());
                    mServiceList.add(s);
                }
            }
        }
        notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下

filterText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(!s.toString().isEmpty()) {
            ArrayList<Service> tmp = new ArrayList<>();
            for (Service s : mServices) {
                if (s.getTitle().toLowerCase().contains(s.toString())) {
                    tmp.add(s);
                }
            }
            mServiceAdapter = new ServiceAdapter(ServiceListActivity.this, tmp);
            mServicesListView.setAdapter(mServiceAdapter);
        } else {
            mServiceAdapter = new ServiceAdapter(ServiceListActivity.this, mServices);
            mServicesListView.setAdapter(mServiceAdapter);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});