将搜索结果绑定到recyclerview

时间:2018-02-15 06:12:26

标签: android android-recyclerview retrofit

我在日志中收到了我的搜索响应,但我无法在我的回收站视图中显示它,我收到了响应但无法绑定到我的回收站视图请帮助我完成此操作,我的日志 E /搜索:::

 "[{\"id\":12,\"category_id\":1,\"status\":\"Rent\",\"type\":\"Residential\",\"gpslat\":\"60.46800000\",\"gpslng\":\"57.45000000\",\"description\":\"Test Description\",\"built_year\":\"2018-10-10 0}]

我的搜索活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchbar);

    session = new Session(this);
    final RecyclerView recyclerView = (RecyclerView)findViewById(R.id.search_result_recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    searchRv =findViewById(R.id.popular_cities_recyclerView);


    Button button = (Button) findViewById(R.id.search_result);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText search = (EditText) findViewById(R.id.searchBar);
            final String keyword = search.getText().toString();
            apiInterface = ApiClient.getClient().create(ApiInterface.class);
            Call<ResponseBody> propCall = apiInterface.getSearchResults(new Session(SearchActivity.this).getJwtToken(),keyword);
            propCall.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> propCall, Response<ResponseBody> response) {

                    String propertyList = null;
                    try {
                        propertyList = response.body().string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String propertiesResults = null;
                    try {
                        propertiesResults = response.body().string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
                    String jsonOutput = null;
                    jsonOutput = gson.toJson(propertyList);
                    Log.e("Search::", jsonOutput);




                }
                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    Log.e("Error", t.getMessage());
                }
            });
        }
    });
}

}

Search Activity

公共类SearchAdapter扩展了RecyclerView.Adapter {     私有上下文;     private List propertyList;

public SearchAdapter(Context context, List<Property> propertyList) {
    this.context = context;
    this.propertyList = propertyList;
}

@Override
public SearchAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.property_list_card_items,parent,false);
    ViewHolder viewHolder=new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(SearchAdapter.ViewHolder holder, int position) {

    Property property = propertyList.get(position);

    List<Image> imageList = property.getImages();

    //Property Viewholder
    holder.prop_tv_price.setText(property.getPrice());
    holder.popular_tv_location.setText(property.getLocation().getArea() + ", " + property.getLocation().getCity());

    //Image List Loading
    String thumbnail = "";
    for (Image image : imageList) {
        if (image.isThumbnail())
            thumbnail = image.getUrl();
    }
    Glide.with(context)
            .load(thumbnail)
            .into(holder.prop_image);



}

@Override
public int getItemCount() {

    return propertyList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView prop_image, agent_imageView, imageView_bedroom, imageView_bathroom, imageView_parking;
    public TextView prop_tv_price, popular_tv_location, textView_bathroom, textView_bedroom, textView_parking;

    public ViewHolder(View itemView) {
        super(itemView);
        prop_image = itemView.findViewById(R.id.property_imageView);
        prop_tv_price = itemView.findViewById(R.id.property_price);
        popular_tv_location = itemView.findViewById(R.id.property_location);
        agent_imageView = itemView.findViewById(R.id.property_agent_imageView);
        imageView_bedroom = itemView.findViewById(R.id.imageview_bedroom_property_card_items);
        imageView_bathroom = itemView.findViewById(R.id.imageview_bathroom_property_card_items);
        imageView_parking = itemView.findViewById(R.id.imageview_parking_property_card_items);
        textView_bathroom = itemView.findViewById(R.id.property_bathroom);
        textView_bedroom = itemView.findViewById(R.id.property_bedroom);
        textView_parking = itemView.findViewById(R.id.property_parking);
    }
}

}

接口

    @GET("/api/search")
Call<ResponseBody> getSearchResults(@Header("Authorization") String accessToken, @Query("q") String Query);

请帮助我``

1 个答案:

答案 0 :(得分:0)

您需要将改装响应模型更改为List<Property>并将此列表传递给您的回收站视图适配器,即

{
    private SearchAdapter searchAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchbar);

        session = new Session(this);
        final RecyclerView recyclerView = (RecyclerView)findViewById(R.id.search_result_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        searchRv =findViewById(R.id.popular_cities_recyclerView);


        Button button = (Button) findViewById(R.id.search_result);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText search = (EditText) findViewById(R.id.searchBar);
                final String keyword = search.getText().toString();
                apiInterface = ApiClient.getClient().create(ApiInterface.class);
                Call<List<Property>> propCall = apiInterface.getSearchResults(new Session(SearchActivity.this).getJwtToken(),keyword);
                propCall.enqueue(new Callback<List<Property>>() {
                    @Override
                    public void onResponse(Call<List<Property>> propCall, Response<List<Property>> response) {

                        try {
                            <List<Property>> propertyList = response.body();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        searchAdapter=new SearchAdapter(your_activity.this,propertyList); //replace your_activity.this with your activity
                        recyclerView.setAdapter(searchAdapter);
                    }
                    @Override
                    public void onFailure(Call<List<Property>> call, Throwable t) {
                        Log.e("Error", t.getMessage());
                    }
                });
            }
        });
    }
}

并改变这样的界面

    @GET("/api/search")
Call<List<Property>> getSearchResults(@Header("Authorization") String accessToken, @Query("q") String Query);

在搜索适配器中进行此更改

if(!thumbnail.equals(""){
Glide.with(context)
        .load(thumbnail)
        .into(holder.prop_image);
}