Java 为ExpandableListView适配器实现过滤器功能
我想在ExpandableListView适配器中过滤数据,但基本上我不知道如何做到这一点。我看了很多教程,但是,也许我一开始就有很多复杂的适配器 这是我的适配器:Java 为ExpandableListView适配器实现过滤器功能,java,android,filtering,adapter,expandablelistview,Java,Android,Filtering,Adapter,Expandablelistview,我想在ExpandableListView适配器中过滤数据,但基本上我不知道如何做到这一点。我看了很多教程,但是,也许我一开始就有很多复杂的适配器 这是我的适配器: package com.example.senso.tutuapp; import android.content.Context; import android.graphics.Typeface; import android.util.Log; import android.view.LayoutInflater; impo
package com.example.senso.tutuapp;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListAdapter extends BaseExpandableListAdapter implements Filterable {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lgroupItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lgroupHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void notifyDataSetInvalidated()
{
super.notifyDataSetInvalidated();
}
@Override
public Filter getFilter() {
return null;
}
public void filterData(String query){
//TODO::No idea how to filter this
query = query.toLowerCase();
notifyDataSetChanged();
}
}
package com.example.senso.tutuapp;
导入android.content.Context;
导入android.graphics.Typeface;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.Filter;
导入android.widget.Filterable;
导入android.widget.TextView;
导入java.util.HashMap;
导入java.util.List;
公共类ExpandableListAdapter扩展BaseExpandableListAdapter实现可过滤{
私人语境(private Context)(私人语境);;
私有列表_listDataHeader;//头标题
//标题标题、子标题格式的子数据
私有HashMap_listDataChild;
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData){
这._context=context;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView
.findViewById(R.id.lgroupItem);
setText(childText);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
TextView lblistheader=(TextView)convertView
.findViewById(R.id.lgroupHeader);
lblListHeader.setTypeface(null,Typeface.BOLD);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
public void notifyDataSetionValidated()
{
super.notifyDataSetionValidated();
}
@凌驾
公共过滤器getFilter(){
返回null;
}
公共void filterData(字符串查询){
//TODO::不知道如何过滤此内容
query=query.toLowerCase();
notifyDataSetChanged();
}
}
下面是一个示例,我基本上是如何使用expandablelistview的
public class WorkActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.work_layout);
Intent newActivity1 = new Intent();
setResult(RESULT_OK,newActivity1);
expListView = (ExpandableListView)findViewById(R.id.expandable_list_view);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) findViewById(R.id.searcher);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
}
});
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Станция отправления");
listDataHeader.add("Станция прибытия");
listDataHeader.add("Дата отправления");
// Adding child data
List<String> departStation = new ArrayList<String>();
departStation.add("Test1");
departStation.add("fffff");
List<String> arriveStation = new ArrayList<String>();
arriveStation.add("Test2");
List<String> data = new ArrayList<String>();
data.add("Test3");
listDataChild.put(listDataHeader.get(0), departStation); // Header, Child data
listDataChild.put(listDataHeader.get(1), arriveStation);
listDataChild.put(listDataHeader.get(2), data);}
公共类WorkActivity扩展活动{
可扩展列表适配器;
ExpandableListView解释视图;
列表列表数据头;
HashMapListDataChild;
搜索视图搜索视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.work\u布局);
Intent newActivity1=新Intent();
setResult(RESULT_OK,newActivity1);
expListView=(ExpandableListView)findViewById(R.id.expandables\u列表\u视图);
//准备列表数据
prepareListData();
listAdapter=新的ExpandableListAdapter(此,listDataHeader,listDataChild);
//设置列表适配器
expListView.setAdapter(listAdapter);
SearchManager SearchManager=(SearchManager)getSystemService(Context.SEARCH\u服务);
searchView=(searchView)findviewbyd(R.id.searcher);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName());
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(新的searchView.OnQueryTextListener(){
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
返回false;
}
});
expListView.setOnGroupExpandListener(新的OnGroupExpandListener(){
@凌驾
public void onGroupExpand(int groupPosition){
}
});
}
私有void prepareListData(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
//添加子数据
列表数据标题。添加(“Саааааааааааааа107;
列表数据标题。添加(“Саааабббббббббб;
listDataHeader.添加(“аааааааааааааааааааа107;
//