Пытаюсь освоить autocompletetextview в Android. Написал свой адаптер, который должен выводить имя объекта. Работает, но не поддерживает русский язык
Помогите, чтобы адаптер распознавал русские символы. Код модели:
public class CategoryModel {
private String id;
private String name;
private AuthorizationclientModel authorizationclientModel;
public CategoryModel() {
}
public CategoryModel(String id, String name, AuthorizationclientModel authorizationclientModel) {
this.id = id;
this.name = name;
this.authorizationclientModel = authorizationclientModel;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AuthorizationclientModel getAuthorizationclientModel() {
return authorizationclientModel;
}
public void setAuthorizationclientModel(AuthorizationclientModel authorizationclientModel) {
this.authorizationclientModel = authorizationclientModel;
}
@Override
public String toString() {
return "ProductCategoryModel{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", authorizationclientModel=" + authorizationclientModel +
'}';
}
}
Код адаптера:
public class CategoryNameAdapter extends ArrayAdapter<CategoryModel> {
private List<CategoryModel> categoryModelsFull;
public CategoryNameAdapter(@NonNull Context context, @NonNull List<CategoryModel> categoryModelList) {
super(context, 0, categoryModelList);
categoryModelsFull = new ArrayList<>(categoryModelList);
}
@NonNull
@Override
public Filter getFilter() {
return categoryFilter;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.name_category, parent, false
);
}
TextView textViewName = convertView.findViewById(R.id.name_category_adapter);
CategoryModel categoryModel = getItem(position);
if (categoryModel != null) {
textViewName.setText(categoryModel.getName());
}
return convertView;
}
private Filter categoryFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<CategoryModel>suggestions = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
suggestions.addAll(categoryModelsFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (CategoryModel item : categoryModelsFull) {
if (item.getName().toLowerCase().contains(filterPattern)) {
suggestions.add(item);
}
}
}
results.values = suggestions;
results.count = suggestions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
addAll((List) results.values);
notifyDataSetChanged();
}
@Override
public CharSequence convertResultToString(Object resultValue) {
return ((CategoryModel) resultValue).getName();
}
};
}
Функция, которая парсит json:
public void loadCategory(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, URLCategory,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for(int i =0;i<jsonArray.length();i++) {
JSONObject categoryObject = jsonArray.getJSONObject(i);
JSONObject authorizationClientObject = categoryObject.getJSONObject("authorizationClientCategory");
CategoryModel categoryModel = new CategoryModel();
AuthorizationclientModel authorizationclientModel = new AuthorizationclientModel();
authorizationclientModel.setId(authorizationClientObject.getString("id"));
authorizationclientModel.setUsername(authorizationClientObject.getString("username"));
authorizationclientModel.setPassword(authorizationClientObject.getString("password"));
categoryModel.setId(categoryObject.getString("id").toString());
categoryModel.setName(categoryObject.getString("name").toString());
categoryModel.setAuthorizationclientModel(authorizationclientModel);
categoryModelList.add(categoryModel);
}
autoCompleteTextViewCategory.setThreshold(1);
categoryAdapter = new CategoryNameAdapter(NewPurchases.this,categoryModelList);
autoCompleteTextViewCategory.setAdapter(categoryAdapter);
autoCompleteTextViewCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CategoryModel categoryModel = (CategoryModel) parent.getItemAtPosition(position);
ID_Category=categoryModel.getId();
loadProduct();
}
});
}
catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(NewPurchases.this,"Server Error",Toast.LENGTH_SHORT).show();
}
}
);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
