sandeep03edu

Search Action

Apr 21st, 2023 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. public class AppUserSearchActivity extends AppCompatActivity {
  2.  
  3.     private static final String TAG = "AppUserSearchActivityTag";
  4.     RecyclerView recyclerView;
  5.     EditText searchEt;
  6.  
  7.     UserDetails myUser;
  8.  
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_app_user_search);
  13.  
  14.         _init();
  15.  
  16.         SearchAction();
  17.     }
  18.  
  19.     private void SearchAction() {
  20.         searchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  21.             @Override
  22.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  23.                 String str = searchEt.getText().toString().trim();
  24.                 Log.d(TAG, "onEditorAction: " + str);
  25.                 Log.d(TAG, "onEditorAction: Action: " + actionId);
  26.                 if (actionId == EditorInfo.IME_ACTION_DONE ) {
  27.                     str = str.toLowerCase();
  28.                     Log.d(TAG, "onEditorAction: Done!!");
  29.  
  30.                     FirebaseFirestore.getInstance().collection(Constants.FIRESTORE_USERS)
  31.                             .whereEqualTo("username", str)
  32.                             .get()
  33.                             .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
  34.                                 @Override
  35.                                 public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
  36.                                     ArrayList<UserDetails> userDetails = new ArrayList<>();
  37.                                     Log.d(TAG, "onSuccess: SearchSize: " + queryDocumentSnapshots.size());
  38.  
  39.                                     for (QueryDocumentSnapshot snapshot : queryDocumentSnapshots) {
  40.                                         UserDetails userDetail = snapshot.toObject(UserDetails.class);
  41.                                         if(!userDetail.getPhone().equalsIgnoreCase(myUser.getPhone())) {
  42.                                             userDetails.add(userDetail);
  43.                                         }
  44.                                     }
  45.  
  46.                                     AppUserSearchAdapter adapter = new AppUserSearchAdapter(AppUserSearchActivity.this, userDetails);
  47.                                     recyclerView.setAdapter(adapter);
  48.                                 }
  49.                             });
  50.  
  51.                     return true;
  52.                 }
  53.                 return false;
  54.             }
  55.         });
  56.  
  57.         searchEt.addTextChangedListener(new TextWatcher() {
  58.             @Override
  59.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  60.  
  61.             }
  62.  
  63.             @Override
  64.             public void onTextChanged(CharSequence s, int start, int before, int count) {
  65.  
  66.             }
  67.  
  68.             @Override
  69.             public void afterTextChanged(Editable s) {
  70.                 ArrayList<UserDetails> userDetails = new ArrayList<>();
  71.                 AppUserSearchAdapter adapter = new AppUserSearchAdapter(AppUserSearchActivity.this, userDetails);
  72.                 recyclerView.setAdapter(adapter);
  73.             }
  74.         });
  75.     }
  76.  
  77.     private void _init() {
  78.         recyclerView = findViewById(R.id.app_user_search_rcv);
  79.         recyclerView.setLayoutManager(new LinearLayoutManager(this));
  80.         searchEt = findViewById(R.id.app_user_search_username);
  81.  
  82.         myUser = Constants.getUser(this);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment