这段代码完全有效,代码是:
这是MainActivity.java
package com.example.tesskian;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.ExpandableListView.OnChildClickListener;
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener, SearchView.OnCloseListener{
private SearchView search;
private mylistadapter listAdapter;
private ExpandableListView myList;
private ArrayList<continent> continentList = new ArrayList<continent>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
//display the list
displayList();
//expand all Groups
expandAll();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//method to expand all groups
private void expandAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++){
myList.expandGroup(i);
}
}
//method to expand all groups
private void displayList() {
//display the list
loadSomeData();
//get reference to the ExpandableListView
myList = (ExpandableListView) findViewById(R.id.expandableList);
//create the adapter by passing your ArrayList data
listAdapter = new mylistadapter(MainActivity.this, continentList);
//attach the adapter to the list
myList.setAdapter(listAdapter);
myList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
if (groupPosition == 0 ) {
if (childPosition == 0){
Intent i = new Intent(getApplicationContext(), cont.class);
startActivity(i);
}
if (childPosition == 1) {
Intent i = new Intent(getApplicationContext(), con1.class);
startActivity(i);
}
if (childPosition == 2) {
Intent i = new Intent(getApplicationContext(), schema.class);
startActivity(i);
}
if (childPosition == 3) {
Intent i = new Intent(getApplicationContext(), ajax.class);
startActivity(i);
}
if (childPosition == 4) {
Intent i = new Intent(getApplicationContext(), examples.class);
startActivity(i);
}
}
if (groupPosition == 1) {
if (childPosition == 0) {
Intent j = new Intent(getApplicationContext(), overview.class);
startActivity(j);
}
if (childPosition == 1) {
Intent j = new Intent(getApplicationContext(), arrai.class);
startActivity(j);
}
if (childPosition == 2) {
Intent j = new Intent(getApplicationContext(), bool.class);
startActivity(j);
}
if (childPosition == 3) {
Intent j = new Intent(getApplicationContext(), object.class);
startActivity(j);
}
if (childPosition == 4) {
Intent j = new Intent(getApplicationContext(), number.class);
startActivity(j);
}
if (childPosition == 5) {
Intent j = new Intent(getApplicationContext(), string.class);
startActivity(j);
}
if (childPosition == 6) {
Intent j = new Intent(getApplicationContext(), tru.class);
startActivity(j);
}
if (childPosition == 7) {
Intent j = new Intent(getApplicationContext(), fals.class);
startActivity(j);
}
if (childPosition == 8) {
Intent j = new Intent(getApplicationContext(), nulll.class);
startActivity(j);
}
}
return false;
}
});
}
private void loadSomeData() {
ArrayList<country> countryList = new ArrayList<country>();
country country = new country("Introduction");
countryList.add(country);
country = new country("Data Types");
countryList.add(country);
country = new country("Schema");
countryList.add(country);
country = new country("Use In Ajax");
countryList.add(country);
country = new country("Examples");
countryList.add(country);
continent continent = new continent("Basics",countryList);
continentList.add(continent);
countryList = new ArrayList<country>();
country = new country("Overview");
countryList.add(country);
country = new country("Array");
countryList.add(country);
country = new country("Boolean");
countryList.add(country);
country = new country("Object");
countryList.add(country);
country = new country("Number");
countryList.add(country);
country = new country("String");
countryList.add(country);
country = new country("False");
countryList.add(country);
country = new country("True");
countryList.add(country);
country = new country("Null");
countryList.add(country);
continent = new continent("Data Types",countryList);
continentList.add(continent);
}
@Override
public boolean onClose() {
listAdapter.filterData("");
expandAll();
return false;
}
@Override
public boolean onQueryTextChange(String query) {
listAdapter.filterData(query);
expandAll();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
listAdapter.filterData(query);
expandAll();
return false;
}
}
这是MyListAdapter.java
package com.example.tesskian;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class mylistadapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<continent> continentList;
private ArrayList<continent> originalList;
public mylistadapter(Context context, ArrayList<continent> continentList) {
this.context = context;
this.continentList = new ArrayList<continent>();
this.continentList.addAll(continentList);
this.originalList = new ArrayList<continent>();
this.originalList.addAll(continentList);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<country> countryList = continentList.get(groupPosition).getCountryList();
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) {
country country = (country) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.child, null);
}
TextView code = (TextView) view.findViewById(R.id.code);
code.setText(country.getCode().trim());
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<country> countryList = continentList.get(groupPosition).getCountryList();
return countryList.size();
}
@Override
public Object getGroup(int groupPosition) {
return continentList.get(groupPosition);
}
@Override
public int getGroupCount() {
return continentList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
continent continent = (continent) getGroup(groupPosition);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.group, null);
}
TextView heading = (TextView) view.findViewById(R.id.heading);
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){
query = query.toLowerCase();
Log.v("MyListAdapter", String.valueOf(continentList.size()));
continentList.clear();
if(query.isEmpty()){
continentList.addAll(originalList);
}
else {
for(continent continent: originalList){
ArrayList<country> countryList = continent.getCountryList();
ArrayList<country> newList = new ArrayList<country>();
for(country country: countryList){
if(country.getCode().toLowerCase().contains(query) ||
country.getName().toLowerCase().contains(query)){
newList.add(country);
}
}
if(newList.size() > 0){
continent nContinent = new continent(continent.getName(),newList);
continentList.add(nContinent);
}
}
}
Log.v("MyListAdapter", String.valueOf(continentList.size()));
notifyDataSetChanged();
}
}
这是Continent.java
package com.example.tesskian;
import java.util.ArrayList;
public class continent {
private String name;
private ArrayList<country> countryList = new ArrayList<country>();
public continent(String name, ArrayList<country> countryList) {
super();
this.name = name;
this.countryList = countryList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<country> getCountryList() {
return countryList;
}
public void setCountryList(ArrayList<country> countryList) {
this.countryList = countryList;
};
}
这是Counter.java
package com.example.tesskian;
public class country {
private String code = "";
private String name = "";
private int population = 0;
public country(String code) {
super();
this.code = code;
this.name = name;
this.population = population;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
它正在工作但是当我开始用searchview过滤它时,问题就来了。活动已经开始,但它与我搜索的活动不匹配。例如,我正在寻找&#34;示例&#34;但是,如果我点击它显示第一个活动&#34; cont.class&#34;。有人能帮助我吗?