Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. public class Frag_SearchFragment extends Fragment {
  2.  
  3. private final static String TAG = Frag_SearchFragment.class.getName().toString();
  4.  
  5. ListView listView;
  6. EditText searcher;
  7. private String uid;
  8. private String displayname ;
  9. private String address ;
  10. private String postcode ;
  11. private String description;
  12. private String contactNo;
  13. private String image ;
  14. private String email;
  15. private String name;
  16. private Boolean isBusiness;
  17. private shopListAdapter adapter;
  18. ArrayList<HairShop> shop = new ArrayList<>();
  19.  
  20. databaseHelper helper;
  21.  
  22. private View searchFragView;
  23.  
  24.  
  25. private DatabaseReference listref;
  26.  
  27. public Frag_SearchFragment() {
  28. }
  29.  
  30. @Override
  31. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  32. Bundle savedInstanceState) {
  33.  
  34. searchFragView = inflater.inflate(R.layout.fragment_frag__search, container, false);
  35. init();
  36. readFirebaseData();
  37.  
  38.  
  39. return searchFragView;
  40. }
  41.  
  42. private void init() {
  43. listView = (ListView) searchFragView.findViewById(R.id.listShopView);
  44. searcher = (EditText) searchFragView.findViewById(R.id.search_tf);
  45. listref = FirebaseDatabase.getInstance().getReference().child("businessAcc");
  46.  
  47.  
  48. }
  49.  
  50. private void readFirebaseData() { <------------------IT is where I think the problem is.
  51. FirebaseDatabase.getInstance().getReference("businessAcc").addListenerForSingleValueEvent(new ValueEventListener() {
  52. @Override
  53. public void onDataChange(DataSnapshot dataSnapshot) {
  54. for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
  55. uid = (String) snapshot.child("uid").getValue();
  56. displayname = (String) snapshot.child("username").getValue();
  57. address = (String) snapshot.child("address").getValue();
  58. postcode = (String) snapshot.child("postCode").getValue();
  59. description = (String) snapshot.child("description").getValue();
  60. contactNo = (String) snapshot.child("contactNo").getValue();
  61. image = (String) snapshot.child("image").getValue();
  62. email = (String) snapshot.child("email").getValue();
  63. name = (String) snapshot.child("name").getValue();
  64. isBusiness = (Boolean) snapshot.child("isBusiness").getValue();
  65.  
  66.  
  67.  
  68. HairShop shops = new HairShop(uid, displayname, address, postcode, contactNo, description, image, email, isBusiness);
  69. shop.add(shops);
  70.  
  71. shopListAdapter adapter = new shopListAdapter(getActivity(), shop);
  72.  
  73. listView.setAdapter(adapter);
  74. filter(adapter);
  75. }
  76. }
  77.  
  78. @Override
  79. public void onCancelled(DatabaseError databaseError) {
  80.  
  81. }
  82.  
  83. });
  84.  
  85.  
  86. }
  87.  
  88. private void filter(final shopListAdapter adapter) {
  89. searcher.addTextChangedListener(new TextWatcher() {
  90. @Override
  91. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  92.  
  93. }
  94.  
  95. @Override
  96. public void onTextChanged(CharSequence s, int start, int before, int count) {
  97.  
  98. adapter.getFilter().filter(s.toString());
  99. }
  100.  
  101.  
  102. @Override
  103. public void afterTextChanged(Editable s) {
  104.  
  105. }
  106. });
  107. }
  108.  
  109. public class HairShop {
  110.  
  111. private String shopID;
  112. private String name;
  113. private String address;
  114. private String image;
  115. private String postCode;
  116. private String description;
  117. private String photoUri;
  118. private String contNo;
  119. private String email;
  120. private Boolean isBusiness;
  121.  
  122. public HairShop(){
  123.  
  124. }
  125. public HairShop(String shopID, String name, String address, String postCode, String contNo, String description, String photoUri,String email,Boolean isBusiness){
  126. this.shopID = shopID;
  127. this.name = name;
  128. this.address = address;
  129. this.photoUri = photoUri;
  130. this.postCode = postCode;
  131. this.contNo = contNo;
  132. this.description = description;
  133. this.email = email;
  134. this.isBusiness = isBusiness;
  135. }
  136. public String getPhotoUri(){
  137. return photoUri;
  138. }
  139. public void setPhotoUri(){
  140. this.photoUri = photoUri;
  141. }
  142.  
  143. public Boolean getBusiness() {
  144. return isBusiness;
  145. }
  146.  
  147. public void setBusiness(Boolean business) {
  148. isBusiness = business;
  149. }
  150.  
  151. public void setShopID(String shopID) {
  152. this.shopID = shopID;
  153. }
  154.  
  155. public String getName() {
  156. return name;
  157. }
  158.  
  159. public void setName(String name) {
  160. this.name = name;
  161. }
  162.  
  163. public void setAddress(String address) {
  164. this.address = address;
  165. }
  166.  
  167. public String getImage() {
  168. return image;
  169. }
  170.  
  171. public void setImage(String image) {
  172. this.image = image;
  173. }
  174.  
  175. public void setPostCode(String postCode) {
  176. this.postCode = postCode;
  177. }
  178.  
  179. public void setDescription(String description) {
  180. this.description = description;
  181. }
  182.  
  183. public void setPhotoUri(String photoUri) {
  184. this.photoUri = photoUri;
  185. }
  186.  
  187. public void setContNo(String contNo) {
  188. this.contNo = contNo;
  189. }
  190.  
  191. public String getEmail() {
  192. return email;
  193. }
  194.  
  195. public void setEmail(String email) {
  196. this.email = email;
  197. }
  198.  
  199. public String getDescription(){
  200.  
  201. return description;
  202. }
  203. public void setDescription(){
  204. this.description = description;
  205. }
  206. public String getShopID(){
  207. return shopID;
  208. }
  209. public void setShopID(){
  210. this.shopID = shopID;
  211. }
  212. public String getShopName(){
  213. return name;
  214. }
  215. public void setShopName(){
  216. this.name = name;
  217. }
  218. public String getAddress(){
  219. return address;
  220. }
  221. public void setAddress(){
  222. this.address = address;
  223. }
  224. public String getPostCode(){
  225. return postCode;
  226. }
  227. public void setPostCode(){
  228. this.postCode = postCode;
  229. }
  230. public String getContNo(){
  231. return contNo;
  232. }
  233. public void setContNo(){
  234. this.contNo = contNo;
  235. }}
  236.  
  237. public class shopListAdapter extends BaseAdapter implements Filterable{
  238. Context mContext;
  239. LayoutInflater inflater;
  240. private ArrayList<HairShop> filterShop = null;
  241. private ArrayList<HairShop> shop;
  242. private ShopFilter shopfilter = new ShopFilter();
  243.  
  244. public shopListAdapter(Context context,
  245. ArrayList<HairShop> filterShop) {
  246. mContext = context;
  247. this.filterShop = filterShop;
  248. inflater = LayoutInflater.from(mContext);
  249. this.shop = new ArrayList<HairShop>();
  250. this.shop.addAll(filterShop);
  251. this.filterShop = filterShop ;
  252. this.shop = filterShop ;
  253. }
  254.  
  255. @Override
  256. public Filter getFilter() {
  257. if (shopfilter == null)
  258. shopfilter = new ShopFilter();
  259.  
  260. return shopfilter;
  261. }
  262.  
  263. public class ViewHolder {
  264. TextView name;
  265. TextView email;
  266. TextView postcode;
  267.  
  268. }
  269.  
  270. @Override
  271. public int getCount() {
  272. return filterShop.size();
  273. }
  274.  
  275. @Override
  276. public HairShop getItem(int position) {
  277. return filterShop.get(position);
  278. }
  279.  
  280. @Override
  281. public long getItemId(int position) {
  282. return position;
  283. }
  284.  
  285. @Override
  286. public View getView(final int position, View convertView, ViewGroup parent) {
  287. final ViewHolder holder;
  288. if (convertView == null) {
  289. holder = new ViewHolder();
  290. convertView = inflater.inflate(R.layout.item, null);
  291.  
  292. holder.name = (TextView) convertView.findViewById(R.id.txtName);
  293. holder.email = (TextView) convertView.findViewById(R.id.txtEmail);
  294. holder.postcode = (TextView) convertView.findViewById(R.id.postCodeID);
  295.  
  296.  
  297. convertView.setTag(holder);
  298. } else {
  299. holder = (ViewHolder) convertView.getTag();
  300. }
  301. // Set the results into TextViews
  302. holder.name.setText(filterShop.get(position).getName());
  303. holder.email.setText(filterShop.get(position).getAddress());
  304. holder.postcode.setText(filterShop.get(position).getPostCode());
  305.  
  306.  
  307. convertView.setOnClickListener(new OnClickListener() {
  308.  
  309. @Override
  310. public void onClick(View arg0) {
  311.  
  312. Intent intent = new Intent(mContext, newsfeed.class);
  313.  
  314. intent.putExtra("rank",
  315. (filterShop.get(position).getAddress()));
  316.  
  317. intent.putExtra("country",
  318. (filterShop.get(position).getName()));
  319.  
  320. intent.putExtra("population",
  321. (filterShop.get(position).getEmail()));
  322.  
  323. intent.putExtra("flag",
  324. (filterShop.get(position).getPostCode()));
  325.  
  326. mContext.startActivity(intent);
  327. }
  328. });
  329.  
  330. return convertView;
  331. }
  332.  
  333. private class ShopFilter extends Filter {
  334. @Override
  335. protected FilterResults performFiltering(CharSequence constraint) {
  336.  
  337. String filterString = constraint.toString().toLowerCase();
  338.  
  339. FilterResults results = new FilterResults();
  340.  
  341. final ArrayList<HairShop> list = filterShop;
  342.  
  343. int count = list.size();
  344. final ArrayList<HairShop> nlist = new ArrayList<>(count);
  345.  
  346. String name ;
  347.  
  348. for (int i = 0; i < count; i++) {
  349. HairShop shops = list.get(i);
  350. name = shops.getName();
  351. if (name.toLowerCase().contains(filterString)) {
  352. nlist.add(shops);
  353. }
  354. }
  355.  
  356.  
  357. results.values = nlist;
  358. results.count = nlist.size();
  359.  
  360. return results;
  361. }
  362.  
  363. @SuppressWarnings("unchecked")
  364. @Override
  365. protected void publishResults(CharSequence constraint, FilterResults results) {
  366. shop = (ArrayList<HairShop>) results.values;
  367. notifyDataSetChanged();
  368. }
  369.  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement