I don't know if I am just having a brain fart or what, but I'm stumped. I have a listView populated by a string array that incorporates search functionality. When the user clicks a listView Item a dialog box appears with information related to that item. Everything works until I search, the search itself works and bring the correct result. Onclick switch case works without searching. The problem, when a search is entered whatever was searched for now assumes the number 0 spot in the array and shows the information for [0] array Item when the item that was searched for was further down the list. Say I have a array with string a,b,c. The onClick dialogs work by default but when I search for "b" and click on it it shows the information for "a" because b is now at the top of the list and "a" has disappeared.
我不知道我是不是只是一个大脑屁或什么,但我很难过。我有一个listView,由一个包含搜索功能的字符串数组填充。当用户单击listView项时,将出现一个对话框,其中包含与该项相关的信息。一切正常,直到我搜索,搜索本身工作并带来正确的结果。 Onclick开关盒无需搜索即可工作。输入搜索时出现的问题现在假设数组中的数字为0,并且当搜索到的项目位于列表的下方时,显示[0]数组项的信息。假设我有一个包含字符串a,b,c的数组。 onClick对话框默认工作,但当我搜索“b”并单击它时,它会显示“a”的信息,因为b现在位于列表的顶部,“a”已消失。
<string-array name="letters">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>
Built with
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, getResources()
.getTextArray(R.array.letters));
Search function
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// DoNotCrush.this.adapter.getFilter().filter(s);
}
});
and the onClick
和onClick
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
showDialog();
//do stuff
break;
case 1:
showDialog();
//do stuff
break;
case 2:
showDialog();
//do stuff
break;
}
}
});
I am assuming it is all in the switch statement and to not use the position variable, but I don't know what else t use since I don;t think you can assign id's to item in a string array. Any help would be appreciated just point me in the right direction.
我假设它全部在switch语句中并且不使用位置变量,但我不知道还有什么用,因为我不知道;我认为你可以将id分配给字符串数组中的item。任何帮助将被赞赏只是指出我正确的方向。
Edit: Thanks to JARP I got my code working. I tossed the switch and used a if statement.
编辑:感谢JARP,我的代码正常运行。我扔了开关并使用了if语句。
String a = parent.getAdapter().getItem(position).toString(); <-JARP'S idea
if (a.equals("Some string")){
//Do This
}else if(a.equals("Another string")){
//Do This
}
This way seems like a lot of work as my listview has over 100 possibilities, but is the only way I could get it to work with the searchable edit text.
这种方式似乎很多工作,因为我的listview有超过100种可能性,但这是我可以使用可搜索的编辑文本的唯一方法。
1 个解决方案
#1
You could use adapter.getItem(position)
你可以使用adapter.getItem(position)
http://developer.android.com/reference/android/widget/Adapter.html#getItem(int)
#1
You could use adapter.getItem(position)
你可以使用adapter.getItem(position)
http://developer.android.com/reference/android/widget/Adapter.html#getItem(int)