对于我们的手册应用,我们想添加一个搜索功能。我们需要在操作栏中使用SearchView,您可以在其中键入一些文本。然后在ExpandableListView中只应显示条目,它们包含键入的文本。 通过单击一个结果,将打开搜索到的PDF文件。
感谢阅读(及答案)。
编辑:以下是一些代码:
private void displayList() {
loadSomeData();
//get reference to the ExpandableListView
myList = (ExpandableListView) findViewById(R.id.expandableList);
//create the adapter by passing your ArrayList data
listAdapter = new expSearchAdapter(Search.this, headersList);
//attach the adapter to the list
myList.setAdapter(listAdapter);
myList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
**// Here I need to get the name of the clicked result and open the PDF**
return false;
}
});
}
private void loadSomeData() {
itemList.add(new expRow("Foo"));
itemList.add(new expRow("Foo2"));
itemList.add(new expRow("Foo3"));
headers = new expHeader("Bar", itemList);
headersList.add(headers);
itemList = new ArrayList<expRow>();
itemList.add(new expRow("Foo4"));
itemList.add(new expRow("Foo5"));
itemList.add(new expRow("Foo6"));
headers = new expHeader("Bar2", itemList);
headersList.add(headers);
}
这是我的expSearchAdapter.class
:
public class expSearchAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<expHeader> HeaderList;
private ArrayList<expHeader> originalList;
public expSearchAdapter(Context context, ArrayList<expHeader> hList) {
this.context = context;
this.HeaderList = new ArrayList<expHeader>();
this.HeaderList.addAll(hList);
this.originalList = new ArrayList<expHeader>();
this.originalList.addAll(hList);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<expRow> countryList = HeaderList.get(groupPosition).getHeaderList();
return countryList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
expRow rowL = (expRow) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item, null);
}
TextView name = (TextView) view.findViewById(R.id.lblListItem);
name.setText(rowL.getName());
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<expRow> countryList = HeaderList.get(groupPosition).getHeaderList();
return countryList.size();
}
@Override
public Object getGroup(int groupPosition) {
return HeaderList.get(groupPosition);
}
@Override
public int getGroupCount() {
return HeaderList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
expHeader continent = (expHeader) getGroup(groupPosition);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_group, null);
}
TextView heading = (TextView) view.findViewById(R.id.lblListHeader);
heading.setTypeface(null, Typeface.BOLD);
heading.setText(continent.getName().trim());
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void filterData(String query, TextView status){
query = query.toLowerCase();
Log.v("Adapter", String.valueOf(HeaderList.size()));
HeaderList.clear();
if(query.isEmpty()){
HeaderList.addAll(originalList);
}
else {
for(expHeader headers: originalList){
ArrayList<expRow> countryList = headers.getHeaderList();
ArrayList<expRow> newList = new ArrayList<expRow>();
for(expRow country: countryList){
if(country.getName().toLowerCase().contains(query) ||
country.getName().toLowerCase().contains(query)){
newList.add(country);
}
}
if(newList.size() > 0){
expHeader nContinent = new expHeader(headers.getName(),newList);
HeaderList.add(nContinent);
}
}
}
Log.v("expSearchAdapter", String.valueOf(HeaderList.size()));
if (HeaderList.size() == 0) {
try {
status.setText(context.getString(R.string.noResult));
status.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
notifyDataSetChanged();
}
}
以下是expHeader.class
:
import java.util.ArrayList;
public class expHeader {
private String name;
private ArrayList<expRow> headerList = new ArrayList<expRow>();
public expHeader(String name, ArrayList<expRow> countryList) {
super();
this.name = name;
this.headerList = countryList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<expRow> getHeaderList() {
return headerList;
}
public void setCountryList(ArrayList<expRow> headerListcontent) {
this.headerList = headerListcontent;
};
}
以下是expRow.class
:
public class expRow {
private String name = "";
public expRow(String content) {
super();
this.name = content;
}
public String getName() {
return name;
}
public void setName(String content) {
this.name = content;
}
}
这是我的SearchView:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search = (SearchView) findViewById(R.id.search);
search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
search.setQueryHint(getString(R.string.search));
//...
@Override
public boolean onQueryTextChange(String query) {
if (query.length() < 3) {
TextView sInfo = (TextView) findViewById(R.id.statusInfo);
sInfo.setVisibility(View.VISIBLE);
sInfo.setText(getString(R.string.notEnoughSearchLetters));
myList.setVisibility(View.GONE);
} else {
TextView sInfo = (TextView) findViewById(R.id.statusInfo);
sInfo.setVisibility(View.GONE);
myList.setVisibility(View.VISIBLE);
listAdapter.filterData(query, sInfo);
}
expandAll();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
if (query.length() < 3) {
TextView sInfo = (TextView) findViewById(R.id.statusInfo);
sInfo.setVisibility(View.VISIBLE);
sInfo.setText(getString(R.string.notEnoughSearchLetters));
myList.setVisibility(View.GONE);
} else {
TextView sInfo = (TextView) findViewById(R.id.statusInfo);
sInfo.setVisibility(View.GONE);
myList.setVisibility(View.VISIBLE);
listAdapter.filterData(query, sInfo);
}