2014-12-30 2 views
1

Я попытался сделать 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; 
} 
} 

Я хочу, чтобы при поиске «Примеры» это пойти «examples.class». Может кто-нибудь мне помочь?

ответ

0

Скачать исходный код отсюда (Expandablelistview With Searchview In Android)

Вам просто необходимо уведомить об изменении адаптера EditText.

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); 
        } 


       } 

      } 
     }); 

Спасибо!

Смежные вопросы