2014-05-12 7 views
0

J'essaye de faire une liste consultable en utilisant un widget de recherche que j'ai déjà (android: id = "@ + id/searchView1"), et jusqu'ici j'ai seulement trouvé l'aide pour ListViews avec ArrayAdapters et les solutions pour ceux qui n'ont pas travailléRecherche d'un ListView qui utilise SimpleAdapter android

Voici mon code

public class SearchActivity extends Activity { 
ListView listView ; 
// Array of words: source http://www.knittinghelp.com/videos/knitting-glossary 
String[] words = new String[] { 
     "()", 
     "[ ]", 
     "*", 
     "**", 
     "alt", 
     "approx", 
     "beg", 
     "bet", 
     "BO", 
     "CA", 
     "CB", 
     "CC", 
     "cdd", 
     "ch", 
     "cm", 
     "cn", 
     "CO", 
     "cont" 
}; 

// Array of meanings: source http://www.knittinghelp.com/videos/knitting-glossary 
String[] meaning = new String[]{ 
    "work instruction between parentheses, in the place directed", 
    "work instructions between brackets, as many times as directed", 
    "repeat instructions following the single asterisk as directed", 
    "repeat instructions between asterisks, as directed", 
    "alternative", 
    "approximately", 
    "beginning", 
    "between", 
    "Bind off", 
    "colour A", 
    "colour B", 
    "colour C", 
    "centered double decrease. sl2 tog, K1, pass the slipped stitches over (together)", 
    "chain (using crochet hook). Start with a slip knot.", 
    "centimeter(s)", 
    "cable needle: short knitting needle, used as an aid in the twisting of a cable.", 
    "cast on", 
    "continue" 
}; 

/** 
* When the search activity begins, show the view as in the search xml, and list the 
* given values in the list view 
*/ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search); 

    final List<HashMap<String, String>> wordList = new ArrayList<HashMap<String, String>>();   

    for(int i=0; i<words.length; i++){ 
     HashMap<String, String> hashMap = new HashMap<String, String>(); 
     hashMap.put("word", words[i]); 
     hashMap.put("meaning", meaning[i]);   
     wordList.add(hashMap);   
    }   

    final SimpleAdapter adapter = new SimpleAdapter(this, wordList, 
      android.R.layout.simple_list_item_2, 
      new String[] {"word", "meaning"}, 
      new int[] {android.R.id.text1, android.R.id.text2}); 

    ListView listView = (ListView) findViewById(R.id.listView1); 

    listView.setAdapter(adapter); 

    EditText search = (EditText) findViewById(R.id.searchView1); 

    search.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
      SearchActivity.this.adapter.getFilter().filter(s); 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 
    }); 
} 
} 

En OnTextChanged, je reçois une erreur sur l'adaptateur en disant qu'il ne peut pas être résolu. Ce serait génial si quelqu'un pouvait me diriger dans la bonne direction :)

+1

vous voulez rechercher par quoi? – NoXSaeeD

+0

J'ai ajouté plus de code en ce que j'ai omis @NoXSaeeD Je voudrais juste chercher par le mot c'est-à-dire alt – Student94

Répondre

1

Vous devez ajouter une variable d'instance appelée adaptateur dans la classe externe. Basé sur le bloc de code affiché vous devez avoir un adaptateur appelé variable membre SimpleAdapter comme ceci:

public class SearchActivity extends Activity { 
    ListView listView; 
    SimpleAdapter adapter; // this is what you're missing 

parce SearchActivity.this.adapter.getFilter().filter(s), fait référence à une variable d'instance appelée adaptateur de la classe SearchActivity (qui n'existe pas).

Questions connexes