Reputation: 15
Edit: I have found a solution. I have made a search function in Laravel, which was a simple query, then implemented in Android Studio an edittext onquerylistener in which I have called the function from the laravel. And it works perfectly.
I need to implement an invitation function in my Android application but I have no prior knowledge for this. The idea is that I have an Edittext
(or maybe another field) where the user can search for other users, who are registered, and by clicking on them they appear below the search field in a listview. And by clicking on the invite button everyone in the listview gets a notification/invitation.
How can this be done using retrofit?
It should be something like for each character written an API
request is sent and suggestions are shown.
I am using Laravel and Android(java).
I don't have much code written here because I don't know where and how to start.
Here is the request:
@GET("users")
Call<List<User>> getUsers(@Header("Authorization") String token);
Here is the getUsers method so far in the activity:
private void getUsers(){
Log.d(TAG, "onResponse: loadData called");
ApiService service = RetrofitBuilder.getRetrofitInstance().create(ApiService.class);
Call<List<User>> call = service.getUsers("Bearer "+token);
call.enqueue(new Callback<List<User>>() {
@Override
public void onResponse(@NonNull Call<List<User>> call, @NonNull Response<List<User>> response) {
try {
if (response.isSuccessful()){
Log.d(TAG, "onResponse: successful loadData called");
adapter = new UserAdapter(mContext, arrayList);
listView.setAdapter(adapter);
}
else{
Log.d(TAG, "onResponse: " + response.body());
}
}
catch (Exception e){
Toast.makeText(TeamAdminActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(@NonNull Call<List<User>> call, @NonNull Throwable t) {
Log.d("Response", "onFailure: " + t.toString());
}
});
}
Also a snippet of my xml file:
<TextView
android:id="@+id/invite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_vertical_margin"
android:layout_marginTop="36dp"
android:layout_below="@id/team_title"
android:textSize="24sp"
android:textColor="@color/colorPrimary"
android:text="@string/invite_members" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/search"
style="@style/TextInputLayoutAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/invite"
android:layout_marginTop="10dp"
android:layout_marginStart="18dp"
android:layout_marginEnd="18dp"
android:hint="@string/search"
android:textSize="23sp"
app:boxStrokeColor="@color/colorPrimaryDark">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/search_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:hint="@string/event_name"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/invite"
android:layout_toEndOf="@+id/search"/>
<TextView
android:id="@+id/invited"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_vertical_margin"
android:layout_marginTop="36dp"
android:layout_below="@id/search"
android:textSize="24sp"
android:textColor="@color/colorPrimary"
android:text="@string/invited" />
<androidx.core.widget.NestedScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@id/invited">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.core.widget.NestedScrollView>
<Button
android:id="@+id/create_team_btn"
android:layout_width="260dp"
android:layout_height="60dp"
android:layout_below="@+id/scroll_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:layout_marginBottom="14dp"
android:background="@drawable/my_color_button"
android:text="@string/invite"
android:textColor="@color/myWhite" />
Upvotes: 0
Views: 824
Reputation: 311
arrayList ? did you add the response's body into the list when the response is successful ? or just pass the response's body into the adapter Like
adapter = new UserAdapter(mContext, response.body());
Upvotes: 0
Reputation: 381
You can do this by implementing Observer
in your activity or fragment where you want to add invitation functionality.
Create class which extends Observable
:
public class FilterManager extends Observable {
private String query;
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
setChanged();
notifyObservers();
}
}
Suppose you have Fragment for the invitation:
public class InvitationFragment extends Fragment implements Observer
{
//your other code here
@Override
public void update(Observable observable, Object o) {
if(observable instanceof FilterManager){
//retrieve the search value
String result = ((FilterManager)observable).getQuery();
if(!result.isEmpty())
getUsers(result); //call api getUsrs method here
else {
userList = null;
setRecyclerView(getContext());
}
}
}
private void getUsers(String query){
//your code here
}
}
Change your rest api call:
@GET("users/{query}")
Call<List<User>> getUsers(@Header("Authorization") String token,@Path("query") String query);
Upvotes: 0