Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Frag_SearchFragment extends Fragment {
- private final static String TAG = Frag_SearchFragment.class.getName().toString();
- ListView listView;
- EditText searcher;
- private String uid;
- private String displayname ;
- private String address ;
- private String postcode ;
- private String description;
- private String contactNo;
- private String image ;
- private String email;
- private String name;
- private Boolean isBusiness;
- private shopListAdapter adapter;
- ArrayList<HairShop> shop = new ArrayList<>();
- databaseHelper helper;
- private View searchFragView;
- private DatabaseReference listref;
- public Frag_SearchFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- searchFragView = inflater.inflate(R.layout.fragment_frag__search, container, false);
- init();
- readFirebaseData();
- return searchFragView;
- }
- private void init() {
- listView = (ListView) searchFragView.findViewById(R.id.listShopView);
- searcher = (EditText) searchFragView.findViewById(R.id.search_tf);
- listref = FirebaseDatabase.getInstance().getReference().child("businessAcc");
- }
- private void readFirebaseData() { <------------------IT is where I think the problem is.
- FirebaseDatabase.getInstance().getReference("businessAcc").addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
- uid = (String) snapshot.child("uid").getValue();
- displayname = (String) snapshot.child("username").getValue();
- address = (String) snapshot.child("address").getValue();
- postcode = (String) snapshot.child("postCode").getValue();
- description = (String) snapshot.child("description").getValue();
- contactNo = (String) snapshot.child("contactNo").getValue();
- image = (String) snapshot.child("image").getValue();
- email = (String) snapshot.child("email").getValue();
- name = (String) snapshot.child("name").getValue();
- isBusiness = (Boolean) snapshot.child("isBusiness").getValue();
- HairShop shops = new HairShop(uid, displayname, address, postcode, contactNo, description, image, email, isBusiness);
- shop.add(shops);
- shopListAdapter adapter = new shopListAdapter(getActivity(), shop);
- listView.setAdapter(adapter);
- filter(adapter);
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
- private void filter(final shopListAdapter adapter) {
- searcher.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) {
- adapter.getFilter().filter(s.toString());
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
- }
- public class HairShop {
- private String shopID;
- private String name;
- private String address;
- private String image;
- private String postCode;
- private String description;
- private String photoUri;
- private String contNo;
- private String email;
- private Boolean isBusiness;
- public HairShop(){
- }
- public HairShop(String shopID, String name, String address, String postCode, String contNo, String description, String photoUri,String email,Boolean isBusiness){
- this.shopID = shopID;
- this.name = name;
- this.address = address;
- this.photoUri = photoUri;
- this.postCode = postCode;
- this.contNo = contNo;
- this.description = description;
- this.email = email;
- this.isBusiness = isBusiness;
- }
- public String getPhotoUri(){
- return photoUri;
- }
- public void setPhotoUri(){
- this.photoUri = photoUri;
- }
- public Boolean getBusiness() {
- return isBusiness;
- }
- public void setBusiness(Boolean business) {
- isBusiness = business;
- }
- public void setShopID(String shopID) {
- this.shopID = shopID;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String getImage() {
- return image;
- }
- public void setImage(String image) {
- this.image = image;
- }
- public void setPostCode(String postCode) {
- this.postCode = postCode;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public void setPhotoUri(String photoUri) {
- this.photoUri = photoUri;
- }
- public void setContNo(String contNo) {
- this.contNo = contNo;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getDescription(){
- return description;
- }
- public void setDescription(){
- this.description = description;
- }
- public String getShopID(){
- return shopID;
- }
- public void setShopID(){
- this.shopID = shopID;
- }
- public String getShopName(){
- return name;
- }
- public void setShopName(){
- this.name = name;
- }
- public String getAddress(){
- return address;
- }
- public void setAddress(){
- this.address = address;
- }
- public String getPostCode(){
- return postCode;
- }
- public void setPostCode(){
- this.postCode = postCode;
- }
- public String getContNo(){
- return contNo;
- }
- public void setContNo(){
- this.contNo = contNo;
- }}
- public class shopListAdapter extends BaseAdapter implements Filterable{
- Context mContext;
- LayoutInflater inflater;
- private ArrayList<HairShop> filterShop = null;
- private ArrayList<HairShop> shop;
- private ShopFilter shopfilter = new ShopFilter();
- public shopListAdapter(Context context,
- ArrayList<HairShop> filterShop) {
- mContext = context;
- this.filterShop = filterShop;
- inflater = LayoutInflater.from(mContext);
- this.shop = new ArrayList<HairShop>();
- this.shop.addAll(filterShop);
- this.filterShop = filterShop ;
- this.shop = filterShop ;
- }
- @Override
- public Filter getFilter() {
- if (shopfilter == null)
- shopfilter = new ShopFilter();
- return shopfilter;
- }
- public class ViewHolder {
- TextView name;
- TextView email;
- TextView postcode;
- }
- @Override
- public int getCount() {
- return filterShop.size();
- }
- @Override
- public HairShop getItem(int position) {
- return filterShop.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- final ViewHolder holder;
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = inflater.inflate(R.layout.item, null);
- holder.name = (TextView) convertView.findViewById(R.id.txtName);
- holder.email = (TextView) convertView.findViewById(R.id.txtEmail);
- holder.postcode = (TextView) convertView.findViewById(R.id.postCodeID);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- // Set the results into TextViews
- holder.name.setText(filterShop.get(position).getName());
- holder.email.setText(filterShop.get(position).getAddress());
- holder.postcode.setText(filterShop.get(position).getPostCode());
- convertView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- Intent intent = new Intent(mContext, newsfeed.class);
- intent.putExtra("rank",
- (filterShop.get(position).getAddress()));
- intent.putExtra("country",
- (filterShop.get(position).getName()));
- intent.putExtra("population",
- (filterShop.get(position).getEmail()));
- intent.putExtra("flag",
- (filterShop.get(position).getPostCode()));
- mContext.startActivity(intent);
- }
- });
- return convertView;
- }
- private class ShopFilter extends Filter {
- @Override
- protected FilterResults performFiltering(CharSequence constraint) {
- String filterString = constraint.toString().toLowerCase();
- FilterResults results = new FilterResults();
- final ArrayList<HairShop> list = filterShop;
- int count = list.size();
- final ArrayList<HairShop> nlist = new ArrayList<>(count);
- String name ;
- for (int i = 0; i < count; i++) {
- HairShop shops = list.get(i);
- name = shops.getName();
- if (name.toLowerCase().contains(filterString)) {
- nlist.add(shops);
- }
- }
- results.values = nlist;
- results.count = nlist.size();
- return results;
- }
- @SuppressWarnings("unchecked")
- @Override
- protected void publishResults(CharSequence constraint, FilterResults results) {
- shop = (ArrayList<HairShop>) results.values;
- notifyDataSetChanged();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement