- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在此 Stackoverflow 中 answer , 这表明过滤可以在 ListView
中完成,而无需覆盖 ArrayAdapter
的 getFilter
方法,而是实现 toString
POJO
类中的。
我已尝试实现它,但过滤功能无法正常工作。尽管 ListView
确实进行了过滤,但它并未显示数组中的正确项。因此,例如,如果过滤器匹配 array
中的一行,则 ListView
中会显示一个项目,但显示的是错误的项目。在这种情况下,始终显示数组的第一项,而不是实际匹配输入的搜索文本的项。
这是我的ArrayAdapter
的代码:
public class TitleListingArrayAdapter extends ArrayAdapter<Title> {
private List<Title> items;
private Context context;
public TitleListingArrayAdapter(Context context, int textViewResourceId, List<Title> items) {
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.titlelisting_single_row, null);
}
Title item = items.get(position);
if (item!= null) {
TextView titleView = (TextView) view.findViewById(R.id.title);
if (titleView != null) {
titleView.setText(item.getName());
}
TextView yearView = (TextView) view.findViewById(R.id.year);
if (yearView != null) {
yearView.setText(String.valueOf(item.getYear())+", ");
}
TextView genreView = (TextView) view.findViewById(R.id.genre);
if (genreView != null) {
genreView.setText(item.getGenre());
}
TextView authorView = (TextView) view.findViewById(R.id.author);
if (authorView != null) {
authorView.setText(item.getAuthor());
}
RatingBar ratingView = (RatingBar) view.findViewById(R.id.rating);
if (ratingView != null) {
ratingView.setRating(item.getRating());
}
ImageView iconView = (ImageView) view.findViewById(R.id.list_image);
iconView.setImageResource(lookupResourceId(context, item.getID()));
}
return view;
}
private int lookupResourceId(Context context, String id) {
String resourceName = "thumb_"+id;
return context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
}
}
这是我的 Activity
代码的相关部分:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing);
databaseHandler = new DatabaseHandler(this);
listView = (ListView) findViewById(R.id.list);
List<Title> titles = databaseHandler.getAllTitles();
adapter = new TitleListingArrayAdapter(this, R.id.list, titles);
listView.setAdapter(adapter);
filterText = (EditText) findViewById(R.id.filter);
filterText.addTextChangedListener(filterTextWatcher);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString().toLowerCase());
}
};
Title POJO
类实现 toString 如下:
@Override
public String toString() {
String name = this.getName() == null ? "" : this.getName().toLowerCase();
String year = this.getYear() == null ? "" : this.getYear().toString();
String genre = this.getGenre() == null ? "" : this.getGenre().toLowerCase();
return name + " " +year+ " "+ genre;
}
有谁知道为什么过滤不能正常工作以及我该如何修复它?
最佳答案
以下question处理与我遇到的完全相同的问题。这个问题还给出了过滤正在做什么的示例,通过不显示列表中的正确项目来显示正确的数量项目。
看来这个answer是错误的,尽管被投票六次。我通过不使用 getFilter
解决了这个问题ArrayAdapter
的方法根本。相反,我创建了一个新的 ArrayAdapter
在我的 TextWatcher
实例如下:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals("")) {
List<Title> filteredTitles = new ArrayList<Title>();
for (int i=0; i<titles.size(); i++) {
if (titles.get(i).toString().contains(s)) {
filteredTitles.add(titles.get(i));
}
}
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, filteredTitles);
listView.setAdapter(adapter);
}
else {
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, titles);
listView.setAdapter(adapter);
}
}
};
请注意,我还移动了声明 List<Title> titles
来自 onCreate
并使它成为我的成员变量 Activity
类,以便可以在 onTextChanged
中访问它filterTextWatcher
的方法| .
关于android - 在不覆盖 getFilter 方法的情况下使用 ArrayAdapter 过滤 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042860/
要更新 Listview 我们可以做以下两件事: 1) 创建一个新的ArrayAdapter,并在每次更新数据时将其绑定(bind)到ListView。 this.listView.setAdapte
基本上我所拥有的是一个显示 ListView 的 fragment 。它目前使用 ArrayAdapter。但是,我正在尝试扩展 ArrayAdapter 以制作我自己的自定义适配器。然后,当我更改代
这真的很奇怪。 当我为 ListView 使用标准 ArrayAdapter 时,调用 setItemChecked 工作正常 但是当使用定制的 ArrayAdapter 时它不会。 原因是什么?这是
我想在我的 android 应用程序中使用 AutoCompleteTextView。我知道如何将它与简单的字符串数组一起使用,但我希望 AutoCompleteTextView 使用对象列表来执行完
问题总结:adapter和spinner都在Spinner Activity中。一旦用户选择了一个名字,它就会将它们发送到主 Activity 。我想从另一个 Activity 中调用 ArrayAd
lang.NullPointerException android.widget.ArrayAdapter.init(ArrayAdapter.java)只是将 Arraylist 值添加到 cust
在开发 Android 应用程序时,我从类似于“Item 1”、“Item 2”、“Item 3”的网站上得到了一行响应我需要将这一行转换为一个列表,以便在 simplelistitem 的数组适配器
您好,我如何检查复选框是否已选中,以及是否已选中我怎样才能删除有问题的行?谢谢你 数组适配器: public class Todoadapter extends ArrayAdapter {
https://developer.android.com/guide/topics/ui/declaring-layout#FillingTheLayout 在这些文档中,我们有以下代码 fragm
我有一个填充数据“neededRepData”的列表,我正在尝试将此列表添加到我的适配器中,但遇到了问题。下面是我的 Reps 类和我的方法(来自另一个类)来迭代所需的 RepData。 public
拥有可以处理多种对象类型的数组适配器的最佳实践是什么? Adapter extends ArrayAdapter 我有 3 种类型的类(class) A.class , B.class , C.cla
我正在制作一个将 JSON 数据解析为 ListView 的应用程序。这对我不起作用;它没有给我任何数据。 这是我的代码: public class MainActivity extends AppC
我有一个 Activity 从另一个 Activity 加载sharedPreference,并从当我编译我的应用程序时,使用 arrayAdpter 将 sharePreferenece 放入 li
我尝试使用 arrayAdapter 自定义将图像和文本加载到 ListView,但加载图像失败 这是我的数组适配器 public class CustomAdapter extends ArrayA
我在使用我的应用程序时遇到问题。我刚刚开始开发该应用程序,到目前为止只花了大约 15 个小时。但我遇到了障碍。通常我可以用谷歌搜索障碍,我的大部分答案都来自这个论坛! 所以我希望我能找到解决方案。下面
我之前曾用其他适配器(如 SimpleAdapter)问过这个问题,但简单适配器的解决方案不适用于 ArrayAdapter。在 SimpleAdapter 中,我获得了屏幕宽度和高度,然后将其传递给
我有 3 个类(class),我希望使用自动完成文本框向用户显示来自 Web 服务(rest api)的某些数据(又名城市)。我已经在我自己的应用程序的各种功能上使用了这个实现,但是由于某种原因,te
我正在从 Google App Engine 获取字符串形式的数据,因此我必须在一个线程中执行此操作。接下来我想在 ListView 中显示它,我正在尝试执行类似以下代码的操作: public voi
我有一个带有自定义 ArrayAdapter 的 ListView ,它设置在一个 fragment 中。仅设置 ListView 中的第一行。 这可能是一般使用带有 fragment 的 ListV
我正在尝试修改 MyKong 中的代码,添加一些更改 TextView 字体的行,但是当我尝试运行时遇到强制关闭。 这里是 MobileArrayAdapter.java > package com.
我是一名优秀的程序员,十分优秀!