在我的actvity中使用TextWatcher类不起作用

时间:2016-03-26 15:42:36

标签: android database listview textwatcher

我试图在我的活动中使用textwatcher。我有一个搜索字段(Pesquisa)。当我在Pesquisa(editText)中键入文本时,应该过滤listview项。但没有变化。这有什么问题?

数据存储在数据库中。当应用程序运行时,Listview显示所有条目并且工作正常。没问题。

我的代码如下:

public class BancoDictionary extends Activity {



public static final String PREFS_NAME = "MyPrefsFile1";



private ListView mListView;


String logTagString="DICTIONARY";
ArrayList<WordDefinition> allWordDefinitions=new ArrayList<WordDefinition>();


// Listview Adapter
    ArrayAdapter<WordDefinition> adapter;

    // Search EditText
    EditText pesquisa;

DictionaryDatabase DictionaryDatabase;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mListView = (ListView) findViewById(R.id.list);

    DictionaryDatabase=new DictionaryDatabase(this); // Puxa a Database da classe DictionaryDatabase
    allWordDefinitions=DictionaryDatabase.getAllWords(); // Pega todas as palavras de Word


    pesquisa = (EditText) findViewById(R.id.EditText01);



    /* Set Text Watcher listener */
    pesquisa.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            BancoDictionary.this.adapter.getFilter().filter(cs); {

                    }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

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


    });


    Collections.sort(allWordDefinitions, new CustomComparator());   // Para Listar em Ordem Alfabética Listview

 // Adding items to listview

    mListView.setEmptyView(findViewById(android.R.id.empty));


    adapter = new ArrayAdapter<WordDefinition>(this, R.layout.list_item, R.id.listItemTextView, allWordDefinitions);
    mListView.setAdapter(adapter);

     if(adapter.getCount()!=0){
         mListView.setAdapter(adapter); 
   }else{
        Toast.makeText(BancoDictionary.this, "No Items Available",Toast.LENGTH_SHORT).show();
   }

            mListView.setAdapter(new BaseAdapter() {

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view==null) {
                view=getLayoutInflater().inflate(R.layout.list_item, parent, false);
            }
            TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
            textView.setText(allWordDefinitions.get(position).word);

            return view;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return allWordDefinitions.size();
        }
    });

    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long arg3) {
            Intent intent =new Intent(BancoDictionary.this, WordDefinitionDetailActivity.class);
            intent.putExtra("word", allWordDefinitions.get(position).word);
            intent.putExtra("definition", allWordDefinitions.get(position).definition);


            startActivity(intent);
        }
    });

}





public class CustomComparator implements Comparator<WordDefinition> { // Para Listar em Ordem Alfabética ListView
       @Override
       public int compare(WordDefinition p1, WordDefinition p2) {
          return p1.word.compareToIgnoreCase(p2.word);
       }
    }

}

0 个答案:

没有答案