我尝试使用搜索过滤器制作expandableListView
。它取得了成功,但是当我搜索它有效时,但是当我点击我搜索的内容时,它会回到第一个活动。
这是完整的MainActivity
:
package com.example.tesskian;
import java.util.ArrayList;
import android.os.Bundle;
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 (id = country("Introduction")) {
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;
}
}
我想在搜索&#34;示例&#34;它会去&#34; examples.class&#34;。 有人能帮助我吗?
答案 0 :(得分:0)
从此处下载源代码(Expandablelistview With Searchview In Android)
您只需要在edittext change上通知适配器。
et_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (et_search.getText().toString().length() == 0) {
customAdapter = new CustomAdapter(lv_country, getApplicationContext());
ev_list.setAdapter(customAdapter);
for (int i = 0; i < customAdapter.getGroupCount(); i++) {
ev_list.expandGroup(i);
}
} else {
List> lv_search = new ArrayList<>();
for (int i = 0; i < lv_country.size(); i++) {
List> lv_searchstate = new ArrayList<>();
List> lv_state = (List>) lv_country.get(i).get("State");
for (int j = 0; j < lv_state.size(); j++) {
if (lv_state.get(j).get("Name").toString().toLowerCase().contains(et_search.getText().toString())) {
lv_searchstate.add(lv_state.get(j));
}
}
if (lv_searchstate.size() != 0) {
HashMap hashMap_search = new HashMap<>();
hashMap_search.put("Name", lv_country.get(i).get("Name").toString());
hashMap_search.put("State", lv_searchstate);
lv_search.add(hashMap_search);
}
}
customAdapter = new CustomAdapter(lv_search, getApplicationContext());
ev_list.setAdapter(customAdapter);
for (int i = 0; i < customAdapter.getGroupCount(); i++) {
ev_list.expandGroup(i);
}
}
}
});
谢谢!