gpt4 book ai didi

java - 尝试在回收者 View 中实现搜索

转载 作者:行者123 更新时间:2023-12-01 19:57:10 30 4
gpt4 key购买 nike

我花了大约两天的时间,但找不到错误。其他一切都正常,但是当我搜索某些内容时,它没有显示任何内容。

这是我的 volley 适配器类

    public class GethubAdapter extends RecyclerView.Adapter<GethubAdapter.GethubViewHolder> implements Filterable {
private List<User> userListFull;
private List<User> userList;


private Context context;

public GethubAdapter(Context context, List<User> userList) {

this.context = context;
this.userList = userList;
userListFull= new ArrayList<>(userList);
}

@NonNull
@Override
public GethubViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.item_user_layout2, parent, false);
return new GethubViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull GethubViewHolder holder, int position) {
User user = userList.get(position);

holder.label.setText(user.getAvatarUrl());
holder.value.setText(String.valueOf(user.getId()));
holder.unit.setText(user.getFollowersUrl());
//Glide.with(holder.imageView.getContext()).load(user.getAvatarUrl()).into(holder.imageView);

}

@Override
public int getItemCount() {
return userList.size();
}

public class GethubViewHolder extends RecyclerView.ViewHolder {


TextView label, value, unit;

public GethubViewHolder(@NonNull View itemView) {
super(itemView);

label = itemView.findViewById(R.id.label);
value = itemView.findViewById(R.id.value);
unit = itemView.findViewById(R.id.unit);
}
}

@Override
public Filter getFilter() {
return userFilter;
}
private Filter userFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<User> filteredList = new ArrayList<>();

if (constraint == null || constraint.length() == 0) {
filteredList.addAll(userListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();

for (User item : userListFull) {
if (item.getId().toString().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}

FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
userList.clear();
userListFull.addAll((List) results.values);
notifyDataSetChanged();
}
};
}

这是我的主要 Activity 类,我使用 gson 将 jason 对象转换为 java 对象

public class MainActivity extends AppCompatActivity {

//private static final String URL = "http://192.168.18.18:1880/mac/user/data.json";
private static final String URL = "https://api.github.com/users";
List<User> light;
GethubAdapter adapter;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.userList);
light = new ArrayList<>();
adapter = new GethubAdapter(this, light);

recyclerView.setLayoutManager(new LinearLayoutManager(this));

StringRequest request = new StringRequest(URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("code", response);
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
//light = (List<User>) gson.fromJson(response,User.class);
List<User> items = new Gson().fromJson(response, new TypeToken<List<User>>() {
}.getType());
Type listType = new TypeToken<ArrayList<User>>() {
}.getType();
List<User> light = new Gson().fromJson(response, listType);
adapter = new GethubAdapter(MainActivity.this, light);
recyclerView.setAdapter(adapter);
//Toast.makeText(MainActivity.this,user[0].getBody(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

RequestQueue queue = Volley.newRequestQueue(this);

queue.add(request);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.example_menu, menu);

MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();

searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {

adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}

我正在使用 volley 库从 api 获取数据

最佳答案

这里:

 protected void publishResults(CharSequence constraint, FilterResults results) {
userList.clear();
userListFull.addAll((List) results.values);
notifyDataSetChanged();
}

您尚未更新用户列表。在那里更新,它的工作原理如下:

protected void publishResults(CharSequence constraint, FilterResults results) {
userList.clear();
userList.addAll((List) results.values);
notifyDataSetChanged();
}

关于java - 尝试在回收者 View 中实现搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59031623/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com