У меня проблема с моим виджетом searchview, я не могу сравнить запрос с объектом, независимо от того, какой путь я выберу (создание списка, который содержит только заголовки и т. Д. c) Как я могу сравнить запрос (в представление поиска) с объектом заголовков и построение нового представления ресайклера, которое будет содержать только запрос?
Это моя исследовательская деятельность:
package com.example.finalhomework.view;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.example.finalhomework.MainActivity;
import com.example.finalhomework.R;
import com.example.finalhomework.adapter.ItemAdapter;
import com.example.finalhomework.model.StackOverflowItem;
import com.example.finalhomework.model.StackOverflowPageResult;
import com.example.finalhomework.network.GetStackOverflowItemDataService;
import com.example.finalhomework.network.RetrofitInstance;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class SearchActivity extends AppCompatActivity {
Toolbar toolbar;
private GetStackOverflowItemDataService stackOverflowItemDataService;
List<StackOverflowItem> stackOverflowItems;
SearchView searchItem;
ItemAdapter adapter;
RecyclerView rvStackoverflow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
setToolbarBack();
searchItem = findViewById(R.id.searchView_searchactivity);
rvStackoverflow = findViewById(R.id.recyclerview_search);
stackOverflowItemDataService = RetrofitInstance.getRetrofitInstance().create(GetStackOverflowItemDataService.class);
stackOverflowItemDataService.getStackOverflowItem("android")
.enqueue(new Callback<StackOverflowPageResult>() {
@Override
public void onResponse(Call<StackOverflowPageResult> call, Response<StackOverflowPageResult> response) {
StackOverflowPageResult stackOverflowPageResult = response.body();
stackOverflowItems = stackOverflowPageResult.getStackOverflowItem();
for (StackOverflowItem s : stackOverflowItems) {
Log.i("Item StackOverflow :", s.getTitle());
}
adapter = new ItemAdapter(stackOverflowItems);
rvStackoverflow.setAdapter(adapter);
rvStackoverflow.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
}
@Override
public void onFailure(Call<StackOverflowPageResult> call, Throwable t) {
}
});
searchItem.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (stackOverflowItems.contains(query)) {
adapter.getFilter().filter(query);
} else
Toast.makeText(SearchActivity.this, "No Match found", Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
private void setToolbarBack() {
toolbar = findViewById(R.id.toolbar_search);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
}
}
Это мой адаптер:
package com.example.finalhomework.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.finalhomework.R;
import com.example.finalhomework.model.StackOverflowItem;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter <ItemAdapter.ItemViewHolder> implements Filterable {
private List <StackOverflowItem> stackOverflowItemList;
public ItemAdapter(List <StackOverflowItem> stackOverflowItemList){
this.stackOverflowItemList = stackOverflowItemList;
}
@NonNull
@Override
public ItemAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(context);
View itemView = layoutInflater.inflate(R.layout.stack_card_view, parent, false);
return new ItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ItemAdapter.ItemViewHolder holder, int position) {
StackOverflowItem stackOverflowItem = stackOverflowItemList.get(position);
holder.itemTitle.setText(stackOverflowItem.getTitle());
holder.link.setText(stackOverflowItem.getLink());
Glide.with(holder.itemView.getContext())
.load(stackOverflowItem.getOwner().getProfileImage())
.diskCacheStrategy(DiskCacheStrategy.ALL) // cache both original & resized image
.centerCrop()
.into(holder.pictureItem);
}
@Override
public int getItemCount() {
return this.stackOverflowItemList.size();
}
@Override
public Filter getFilter() {
return null;
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView itemTitle, link;
private ImageView pictureItem;
public ItemViewHolder(@NonNull View itemView) {
super(itemView);
itemTitle = itemView.findViewById(R.id.textView_item_title);
link = itemView.findViewById(R.id.textView_item_link);
pictureItem = itemView.findViewById(R.id.imageView_item);
}
}
}
Это мой stackoverflowItem, объект, который я ищу:
package com.example.finalhomework.model;
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class StackOverflowItem implements Serializable
{
@SerializedName("tags")
@Expose
private List<String> tags = null;
@SerializedName("owner")
@Expose
private Owner owner;
@SerializedName("is_answered")
@Expose
private Boolean isAnswered;
@SerializedName("view_count")
@Expose
private Integer viewCount;
@SerializedName("answer_count")
@Expose
private Integer answerCount;
@SerializedName("score")
@Expose
private Integer score;
@SerializedName("last_activity_date")
@Expose
private Integer lastActivityDate;
@SerializedName("creation_date")
@Expose
private Integer creationDate;
@SerializedName("question_id")
@Expose
private Integer questionId;
@SerializedName("content_license")
@Expose
private String contentLicense;
@SerializedName("link")
@Expose
private String link;
@SerializedName("title")
@Expose
private String title;
@SerializedName("last_edit_date")
@Expose
private Integer lastEditDate;
@SerializedName("accepted_answer_id")
@Expose
private Integer acceptedAnswerId;
@SerializedName("closed_date")
@Expose
private Integer closedDate;
@SerializedName("closed_reason")
@Expose
private String closedReason;
private final static long serialVersionUID = 2088551364601451752L;
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
public Boolean getIsAnswered() {
return isAnswered;
}
public void setIsAnswered(Boolean isAnswered) {
this.isAnswered = isAnswered;
}
public Integer getViewCount() {
return viewCount;
}
public void setViewCount(Integer viewCount) {
this.viewCount = viewCount;
}
public Integer getAnswerCount() {
return answerCount;
}
public void setAnswerCount(Integer answerCount) {
this.answerCount = answerCount;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public Integer getLastActivityDate() {
return lastActivityDate;
}
public void setLastActivityDate(Integer lastActivityDate) {
this.lastActivityDate = lastActivityDate;
}
public Integer getCreationDate() {
return creationDate;
}
public void setCreationDate(Integer creationDate) {
this.creationDate = creationDate;
}
public Integer getQuestionId() {
return questionId;
}
public void setQuestionId(Integer questionId) {
this.questionId = questionId;
}
public String getContentLicense() {
return contentLicense;
}
public void setContentLicense(String contentLicense) {
this.contentLicense = contentLicense;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getLastEditDate() {
return lastEditDate;
}
public void setLastEditDate(Integer lastEditDate) {
this.lastEditDate = lastEditDate;
}
public Integer getAcceptedAnswerId() {
return acceptedAnswerId;
}
public void setAcceptedAnswerId(Integer acceptedAnswerId) {
this.acceptedAnswerId = acceptedAnswerId;
}
public Integer getClosedDate() {
return closedDate;
}
public void setClosedDate(Integer closedDate) {
this.closedDate = closedDate;
}
public String getClosedReason() {
return closedReason;
}
public void setClosedReason(String closedReason) {
this.closedReason = closedReason;
}
}
Спасибо за помощь