多项选择可搜索列表视图
好的,我有一个可以正常工作的多项选择 ListView。我选中联系人的框(保存在 String[] 中)并且可以正常返回值。因为有些人有很多联系人,所以我想创建一个类似于 Android 电话簿的搜索栏的搜索栏。我创建了一个 EditText 并将其对齐在我的列表上方。我在 StackOverflow 上找到了过滤代码,效果非常好。
我的问题:
当您过滤掉某人的名字并选择该名称时,当您从 EditText 退格或继续键入时,不会保存您选择的名称的正确位置。例如,如果我开始输入“Adam”并进入“Ada”并选择它,如果我退格键输入“Carol”,则选择“Ada”所在的任何位置。它会收集“Adam”点击时所在的位置(假设为 2),并且当列表恢复时会检查该位置 (2),即使 Adam 不再在那里。我需要一种方法来收集姓名。然后,当恢复或再次搜索列表时,会检查亚当的姓名,而不是亚当之前所在的位置。除了创建大量数组之外,我绝对没有任何想法,并且确实需要一些帮助。下面是我正在使用的一些代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
myListView = (ListView)findViewById(android.R.id.list);
search_EditText = (EditText) findViewById(R.id.search_EditText);
search_EditText.addTextChangedListener(filterTextWatcher);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, ContactsList);
setListAdapter(adapter);
myListView.setItemsCanFocus(false);
getListView().setChoiceMode(2);
myListView.setTextFilterEnabled(true);
myListView.setFastScrollEnabled(true);
myListView.invalidate();
}
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);
}
};
OK I have a multiple choice ListView that works fine. I check the boxes for the contacts (held in a String[]) and can return the values fine. Because some people have a bunch of contacts I wanted to create a search bar kind of like the stock one for the Android phone book. I created an EditText and aligned it above my list. I found the filtering code here on StackOverflow and it works wonderfully.
My Problem:
When you filter someones name out, and you select the name, when you either backspace from the EditText or continue typing, the correct position of the name you selected is not saved. For example, if I start typing "Adam" and get to "Ada" and select it, if I backspace to type in "Carol", whatever position "Ada" was at is selected. It gathers the place that "Adam" was at from the click (Let's say 2) and when the list is restored checks that position (2) even though Adam is not there anymore. I need a way to gather the name.. then when the list is restored or searched again, the NAME Adam is checked and not the POSITION Adam was previously at. I have absolutely no ideas other than creating tons of Arrays and could really use some help. Below is some of the code I'm using:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
myListView = (ListView)findViewById(android.R.id.list);
search_EditText = (EditText) findViewById(R.id.search_EditText);
search_EditText.addTextChangedListener(filterTextWatcher);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, ContactsList);
setListAdapter(adapter);
myListView.setItemsCanFocus(false);
getListView().setChoiceMode(2);
myListView.setTextFilterEnabled(true);
myListView.setFastScrollEnabled(true);
myListView.invalidate();
}
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);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从未找到比下面列出的方法更好的方法。我不再使用这段代码了,如果有更好的解决方案,我希望有人将其发布在某个地方。
我最终做的是创建一个 ArrayList 来保存选定的名称。如果选择了该名称,则将该名称推入 ArrayList 中,如果未选中,则从列表中弹出该名称。当选择
afterTextChanged
时,将迭代列表并检查名称当前是否已在适配器中列出。当您完成选择过程并想要继续时,我清除EditText
以清除过滤器,因此在 ListView 中填充完整列表,并将所有联系人设置为选中(如果它们存在于 ArrayList 中) 。请注意,我对仅列出姓名的联系人列表使用自定义适配器,因此如果您使用其他类型的数据,此解决方案可能会变得更加混乱,并且我将此方法视为黑客解决方案:
您不想使用使用过滤器时建立索引,因为索引 1 可能是一个
ListView
中的某项,而当您更改过滤器时,它可能是另一个ListView
项。I never ended up finding a better method than the one I am going to list below. I do not use this code much anymore, and if there is a better solution, I hope someone has it posted somewhere.
What I ended up doing was creating an
ArrayList
to hold the selected names. If the name is selected, the name is pushed into theArrayList
, and if it is unchecked, it is popped from the list. WhenafterTextChanged
is selected, the list is iterated through and the names are checked if they are currently listed in the adapter. When you finish with the selection process and want to continue, I clear theEditText
to clear the filter, therefore populating the complete list in the ListView and setting all of the contacts to selected if they exist in the ArrayList.Note, I use a custom adapter for a list of contacts that only list names, so this solution may get more confusing if you use other types of data, and I look at this method as a hacked solution:
You don't want to use Indexes when using the filters because index 1 might be something in one
ListView
, and it might be anotherListView
item when you change the filter.您可以创建 android 项目并添加以下文件:
res/layout->list_row.xml
res/layout->activity_main.xml
ContactObject.java
ContactsListClass.java
ContactsAdapter.java
MainActivity.java
AndroidManifest.xml
You can create android project and add these files:
res/layout->list_row.xml
res/layout->activity_main.xml
ContactObject.java
ContactsListClass.java
ContactsAdapter.java
MainActivity.java
AndroidManifest.xml