【问题标题】:How to search users from database using retrofit in Android studio? SOLVED如何在 Android Studio 中使用改造从数据库中搜索用户?解决了
【发布时间】:2020-08-06 18:23:50
【问题描述】:

编辑:我找到了解决方案。我在 Laravel 中创建了一个搜索功能,这是一个简单的查询,然后在 Android Studio 中实现了一个 edittext onquerylistener,我在其中从 laravel 调用了该函数。而且效果很好。

我需要在我的 Android 应用程序中实现邀请功能,但我对此一无所知。我的想法是我有一个Edittext(或者可能是另一个字段),用户可以在其中搜索已注册的其他用户,然后单击它们,它们会出现在列表视图中的搜索字段下方。通过单击邀请按钮,列表视图中的每个人都会收到通知/邀请。 如何使用改造来做到这一点? 它应该类似于为每个写入的字符发送API 请求并显示建议。 我正在使用 Laravel 和 Android(java). 我在这里写的代码不多,因为我不知道从哪里开始以及如何开始。 这是请求:

 @GET("users")
 Call<List<User>> getUsers(@Header("Authorization") String token);

这是活动中到目前为止的 getUsers 方法:

  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());
        }
    });
}

也是我的 xml 文件的 sn-p:

 <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" />

【问题讨论】:

  • 看来你有点迷茫了。我建议你看看Guide to app architecture。您需要花一点时间阅读和理解它,但它会帮助您了解基本概念并提供在 Android 中使用数据库和 Web 服务 (Retrofit) 的最佳实践。

标签: android laravel api android-studio retrofit


【解决方案1】:

数组列表?响应成功后,您是否将响应的正文添加到列表中?或者只是将响应的主体传递给适配器 喜欢

adapter = new UserAdapter(mContext, response.body());

【讨论】:

    【解决方案2】:

    您可以通过在您想要添加邀请功能的活动或片段中实现Observer 来做到这一点。

    创建扩展Observable的类:

    public class FilterManager extends Observable {
        private String query;
    
        public String getQuery() {
            return query;
        }
    
        public void setQuery(String query) {
            this.query = query;
            setChanged();
            notifyObservers();
        }
    
    }
    

    假设您有邀请的 Fragment:

    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
        }
    }
    

    更改您的 REST API 调用:

    @GET("users/{query}")
    Call<List<User>> getUsers(@Header("Authorization") String token,@Path("query") String query);
    

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 2019-01-11
      • 2022-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多