【发布时间】:2016-10-26 16:10:56
【问题描述】:
我已经实现了一个带有搜索视图的列表视图。因此,当用户在搜索视图中输入时,它将搜索列表中的匹配条目并相应地更新列表视图以显示匹配条目。
但我想问的是, 有什么方法可以确定,输入到 searchview 中的文本不存在于列表中。这样对于该条目,我可以显示添加按钮以将该条目添加到列表中 现在我的代码是,
addLabelText =(SearchView) findViewById(R.id.addLabelText);
addLabelText.setOnQueryTextListener(this);
listView = (ListView) findViewById(R.id.listview);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,lv_arr);//here lv_arr is String[]
listView.setAdapter(adapter);
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
简而言之,我想动态更改搜索视图的关闭图标, 如果适配器返回一些条目,它应该显示关闭图标...如果它是空的,那么它应该显示 + 图标,以便用户可以将新条目添加到列表中...
我知道,我们可以通过使用来改变搜索视图的关闭图标,
int closeButtonId = getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButtonImage = (ImageView) addLabelText.findViewById(closeButtonId);
closeButtonImage.setImageResource(R.drawable.close);
但是如何写条件来判断适配器是否显示空列表。与否
【问题讨论】:
-
您不必动态进行。您可以通过单独的样式来做到这一点。
-
请您详细说明一下。??
-
在这里查看我的答案。 stackoverflow.com/questions/40124611/…
-
但是,我有一个条件,==>> 被过滤列表包含任何孩子? -->如果是,那么关闭图标应该是“X”.....如果不是,那么关闭图标应该是“+”
-
看这个。 gist.github.com/dbachelder/9587898 和这个 nlopez.io/how-to-style-the-actionbar-searchview-programmatically/
标签: android