- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
以下代码在搜索 EditText
中输入任何字符时在 ListView 中返回 0 个 View 。以下方法来自 Activity 类
private void setupList() {
final ListView lv = (ListView) findViewById(R.id.contactList);
ArrayAdapter<Info> la = new MyListAdapter(this, mInfoList);
lv.setAdapter(la);
lv.setTextFilterEnabled(true);
EditText edit = (EditText) findViewById(R.id.searchbar);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable text) {
Log.d("search", ""+text);
ArrayAdapter<Info> la = (ArrayAdapter<Info>) lv.getAdapter();
la.getFilter().filter(text);
la.notifyDataSetChanged();
}
});
}
这是我的适配器类
public class MyListAdapter extends ArrayAdapter<Info> {
private Bitmap mDefaultProfilePic = null;
Context mContext = null;
public MyListAdapter(Context ctxt, ArrayList<Info> mFriendsAccounts) {
super(ctxt, R.id.name, mFriendsAccounts);
mContext = ctxt;
mDefaultProfilePic = BitmapFactory.decodeResource(ctxt.getResources(), R.drawable.face);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.layout_list_view, null);
}
Info usr = getItem(position);
((TextView)convertView.findViewById(R.id.name)).setText(usr.Name);
((ImageView)convertView.findViewById(R.id.invite)).setTag(position);
if (mImageBitmaps.get(position) != null) {
((ImageView)convertView.findViewById(R.id.profilePic)).setImageBitmap(mImageBitmaps.get(position));
} else {
((ImageView)convertView.findViewById(R.id.profilePic)).setImageBitmap(mDefaultProfilePic);
}
return convertView;
}
}
最佳答案
问题终于解决了。我不得不覆盖 Info
对象中的 toString()
方法。在我的例子中,过滤基于 name
字段,因此通过 toString()
返回它。
过滤过程对适配器中的每个对象调用toString()
。
关于android - 在 String ArrayAdapter 支持的 ListView 上使用 TextWatcher 进行过滤返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6999918/
什么情况下应该使用afterTextChanged而不是onTextChanged,反之亦然? 最佳答案 这些事件按以下顺序调用: beforeTextChanged(CharSequence s,
我正在做一个聊天应用程序,并了解如何制作 textWatcher,它将在用户输入的情况下推送 firebase 数据结构.我想推送一个数据结构,在该数据结构上您将看到用户是否正在输入。当用户键入时,用
我有一个自定义列表适配器,其中一个列表项具有三个编辑文本。我有一个日期选择器供其中之一使用。设置日期后,适配器的相应型号会正确保存日期。但是对于其他编辑文本,我只想将输入的文本存储在列表中。我使用 t
我正在尝试让 TextWatcher 与 AutoCompleteTextView 一起工作,但遇到了一些问题。我面临的问题是,当我开始输入文本时,如果数据输入的长度只有一个字符,我在 AutoCom
自从我开始使用 TextWatcher 以来,我的应用程序不断崩溃...正如你在下面看到的,我为 3 个 EditText 字段创建了一个 TextWatcher...我做了一个按钮来监听 3 个 E
我正在尝试配置一个简单的表单。我有 3 个编辑文本,在用户结束输入后,我想显示选中的图标(如果有效)(并转到下一个字段),如果无效则显示错误,用户可以更正。 我的主要问题是我只能输入 1 个字符,然后
我的 Android 应用程序上有一个 TextWatcher。问题是我希望它监听每个文本更改,并 append 一个相同的 TextView 字符串。 例如,用户在 EditText 字段上输入 A
我已将 TextWatcher 添加到 EditText 并通过 onTextChanged(CharSequence s, int start, int before, int count) 方法监
在我按下退格键之后,我的 textwatcher 开始工作,然后那个建议出现了。我有一个 textwatcher,可以从网络生成书籍列表。我也等着看它是否是一个网络问题。但我发现不是因为那个,当我按退
我正在尝试过滤 gridview,为此我在编辑框中输入数据。它工作得很好,除了当我在编辑框中插入一个空格字符时,它不知何故无法识别它并且结果集为空。请帮忙 这是我的编辑框监听器: search.add
我在 EditText 字段上有提示文本和普通文本,我指定了不同的 Font-Size、Font-Color、Font-Name 两者。我已经把它放在我的 ActivityClass 中,它工作正常。
我有两个编辑文本,我想在编辑另一个文本的同时更改一个文本,反之亦然。我使用了 TextWatcher,它或多或少是这样的: //MUDAR AO DIGITAR edt1.addTe
我是 Android 开发的新手,目前正在开发一款应用程序,该应用程序允许人们在框中键入文本,文本会显示在屏幕上的位图上。 我在监听变化的 Activity 中有一个文本框。每次键入文本时,它都会调用
每次将“q”写入我附加 TextWatcher 的编辑文本的最后一个字符时,“q”就会被“a”替换。我使用: public void afterTextChanged(Editable s) {
我的代码看起来很简单,但我不知道问题出在哪里。如果我编写这样的代码: tvResult.setText(sum+ "RUB"); 它显示了正确的数字。但如果我尝试添加这样的 IF 语句: if(sum
在我的应用程序中,我将 TextWatcher 放在 EditText 上。当我更改 EditText 的文本时,TextWatcher 的事件被调用两次。 我正在使用模拟器来运行该应用程序。 最佳答
我有一个 Android 编程问题。使用下面的代码我想验证一个字符串匹配。它验证正常,但 LogCat 显示 TextWatcher 方法在每次击键时触发两次,我不明白为什么。我希望每次击键只触发一次
TextWatcher 有一个烦人的问题。我一直在网上搜索,但找不到任何东西。如果有人可以帮助我,将不胜感激。 出于某种原因,在一个文本更改时调用 TextWatcher 事件是不稳定的。有时它们会被
在我的应用程序中,我想跟踪 EditText 中的输入。每次用户修改 EditText 中的字符时,我想跟踪: 如果添加/删除了字符 添加/删除字符的索引以及 添加/删除了什么字符 我使用TextWa
我想使用 TextWatcher 为多个 editText 编写一个 validator 。 我已经有方法来验证用户写入的数据是否正确。还添加了验证来检查 editText 是否为空。但这里它没有像我
我是一名优秀的程序员,十分优秀!