在自定义 ListView 中搜索
Search in custom ListView
搜索中的问题是只从第一个词开始
示例:在 "Real Madrid" 中搜索,如果搜索 "Madrid" 没有给出结果,但是如果搜索 "Real" 给我结果,因为单词 "real" 是第一个单词
那么这个问题的解决方案是什么?谢谢:)
public void setAdapterToListview() {
objAdapter = new LatestGridAdapter(getActivity(), R.layout.latest_lsv_item,
arrayOfLatestVideo,columnWidth);
lsv_latest.setAdapter(objAdapter);
}
public void showToast(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
final MenuItem searchMenuItem = menu.findItem(R.id.search);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus) {
searchMenuItem.collapseActionView();
searchView.setQuery("", false);
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(newText.toString().equalsIgnoreCase((String) allArrayVideoName[i].substring(0, textlength)))
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
});
}
尝试this搜索方法。如果它 returns 除 -1 之外的任何其他内容,则表示它找到了匹配项。我把所有的都变成小写了,这样你就不用担心区分大小写了。
@Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(allArrayVideoName[i].toLowerCase().indexOf(newText.toLowerCase())!=-1)
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
搜索中的问题是只从第一个词开始 示例:在 "Real Madrid" 中搜索,如果搜索 "Madrid" 没有给出结果,但是如果搜索 "Real" 给我结果,因为单词 "real" 是第一个单词 那么这个问题的解决方案是什么?谢谢:)
public void setAdapterToListview() {
objAdapter = new LatestGridAdapter(getActivity(), R.layout.latest_lsv_item,
arrayOfLatestVideo,columnWidth);
lsv_latest.setAdapter(objAdapter);
}
public void showToast(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
final MenuItem searchMenuItem = menu.findItem(R.id.search);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus) {
searchMenuItem.collapseActionView();
searchView.setQuery("", false);
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(newText.toString().equalsIgnoreCase((String) allArrayVideoName[i].substring(0, textlength)))
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
});
}
尝试this搜索方法。如果它 returns 除 -1 之外的任何其他内容,则表示它找到了匹配项。我把所有的都变成小写了,这样你就不用担心区分大小写了。
@Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(allArrayVideoName[i].toLowerCase().indexOf(newText.toLowerCase())!=-1)
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}