我有Listview
自定义适配器。我在活动栏中添加了Searchview
来过滤Listview
中的项目。 Mycustom Listview
和Searchview
正常工作。
我希望我的Listview
开头为空,但只需根据输入Searchview
我的代码示例
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView);
btnsrc = (Button) findViewById(R.id.btnSearch);
SeaFoodData food = new SeaFoodData(this);
ArrayList<SeaFood> sList = new ArrayList<SeaFood>();
try {
sList = food.GetSeaFood();
} catch (JSONException e) {
e.printStackTrace();
}
sfAdapter = new SeaFoodAdapter(MainActivity.this, sList);
lv.setAdapter(sfAdapter);
lv.setTextFilterEnabled(true);
filter = sfAdapter.getFilter();
}
@Override
public boolean onQueryTextChange(String newText)
{
filter.filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQueryHint("Ara...");
setupSearchView();
return true;
}
private void setupSearchView()
{
sv.setOnQueryTextListener(this);
}
我的适配器
public SeaFoodAdapter(Context ctx, ArrayList<SeaFood> foodList) {
super();
this.context = ctx;
this.seaFoodArrayList = foodList;
}
public class SeaFoodHolder
{
TextView food;
TextView description;
ImageView rsm;
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<SeaFood> results = new ArrayList<SeaFood>();
if (orig == null)
orig = seaFoodArrayList;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final SeaFood g : orig) {
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
seaFoodArrayList = (ArrayList<SeaFood>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return seaFoodArrayList.size();
}
@Override
public SeaFood getItem(int position) {
return seaFoodArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SeaFoodHolder holder;
if(convertView==null)
{
convertView=LayoutInflater.from(context).inflate(R.layout.row, parent, false);
holder=new SeaFoodHolder();
holder.food=(TextView) convertView.findViewById(R.id.tvFood);
holder.description=(TextView) convertView.findViewById(R.id.tvDescription);
holder.rsm=(ImageView) convertView.findViewById(R.id.warnimage);
convertView.setTag(holder);
}
else
{
holder=(SeaFoodHolder) convertView.getTag();
}
holder.food.setText(seaFoodArrayList.get(position).getName());
holder.description.setText(String.valueOf(seaFoodArrayList.get(position).getDescription()));
if (seaFoodArrayList.get(position).getimgPath().equals("0") ) {
holder.rsm.setImageResource(R.drawable.error);
} else {
holder.rsm.setImageResource(R.drawable.check);
}
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.parseColor("#E3F2FD"));
} else {
convertView.setBackgroundColor(Color.parseColor("#90CAF9"));
}
View vi = convertView;
vi.setOnCreateContextMenuListener(this);
return convertView;
}
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
// empty implementation
}
解决
我解决了这个问题。
首先,
在设置适配器后,我将Listview隐藏在On Create功能中。
sfAdapter = new SeaFoodAdapter(MainActivity.this, sList);
lv.setAdapter(sfAdapter);
lv.setTextFilterEnabled(true);
filter = sfAdapter.getFilter();
lv.setVisibility(View.GONE);
然后,
根据onQueryTextChange函数中的搜索查询显示ListView
@Override
public boolean onQueryTextChange(String newText)
{
if(newText == null || newText.equals(""))
{
lv.setVisibility(View.GONE);
}else{
lv.setVisibility(View.VISIBLE);
}
filter.filter(newText);
return true;
}
希望这对某人有帮助。谢谢你的帮助。
答案 0 :(得分:0)
如果我了解你,只想在搜索字段中没有任何内容时显示空列表视图?
public View getView(int position, View convertView, ViewGroup parent) {
if(searchFieldTextView.getText() == null || searchFieldTextView.getText().equals(""))
{
//Return empty view
}else{
//Return your listview content
}
这对你有意义吗?
希望这有帮助。
编辑:
为了访问它,只需将其声明为静态
MainActivity中的
static SearchView searchField;
你的onCreateOptionsMenu中的只需添加
searchField = searchView;
或
searchfield = (SearchView) menu.findItem(R.id.action_search).getActionView();
然后访问它只需调用
MainActivity.searchField