Cannot use search in onTextChanged() method

 MainActivity.this.sAdapter.getFilter().filter(cs);// cannot find symbol variable sAdapter 

class:

 public class MainActivity extends AppCompatActivity { EditText mSearch; final String ATTRIBUTE_NAME_TEXT = "text"; final String ATTRIBUTE_NAME_PB = "pb"; final String ATTRIBUTE_NAME_LL = "ll"; ListView lvSimple; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int load[] = { 41, 48, 22, 35, 30, 67, 51, 88 }; ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>( load.length); Map<String, Object> m; for (int i = 0; i < load.length; i++) { m = new HashMap<String, Object>(); m.put(ATTRIBUTE_NAME_TEXT, "Day " + (i+1) + ". Load: " + load[i] + "%"); m.put(ATTRIBUTE_NAME_PB, load[i]); m.put(ATTRIBUTE_NAME_LL, load[i]); data.add(m); } String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_PB, ATTRIBUTE_NAME_LL }; int[] to = { R.id.tvLoad, R.id.pbLoad, R.id.llLoad }; SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item, from, to); sAdapter.setViewBinder(new MyViewBinder()); lvSimple = (ListView) findViewById(R.id.lvSimple); lvSimple.setAdapter(sAdapter); //--------------- addTextChangedListener() ------------------------------- mSearch = (EditText) findViewById(R.id.mSearch); mSearch.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { //Error: cannot find symbol variable sAdapter MainActivity.this.sAdapter.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 } }); }// End onCreate() class MyViewBinder implements SimpleAdapter.ViewBinder { int red = getResources().getColor(R.color.Red); int orange = getResources().getColor(R.color.Orange); int green = getResources().getColor(R.color.Green); @Override public boolean setViewValue(View view, Object data, String textRepresentation) { int i = 0; switch (view.getId()) { // LinearLayout case R.id.llLoad: i = ((Integer) data).intValue(); if (i < 40) view.setBackgroundColor(green); else if (i < 70) view.setBackgroundColor(orange); else view.setBackgroundColor(red); return true; // ProgressBar case R.id.pbLoad: i = ((Integer) data).intValue(); ((ProgressBar)view).setProgress(i); return true; } return false; } } } 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <!-- Editext for appearance searching field --> <EditText android:id="@+id/mSearch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint=" Search code.." android:inputType="textVisiblePassword" android:backgroundTint="#fffb20" android:textColorHint="#e4c6c4c4" /> <ListView android:id="@+id/lvSimple" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> 

    1 answer 1

    Your adapter is declared to be a local variable of the onCreate method, the class knows nothing about it. Therefore either

     final SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item, from, to); 

    and

     sAdapter.getFilter().filter(cs); // MainActivity.this - убрать 

    or make sAdapter class variable MainActivity

     private SimpleAdapter sAdapter; 

    and

     sAdapter = new SimpleAdapter(this, data, R.layout.item, from, to); 
    • finally I get "java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size ()' on a null object reference" - mtb
    • @mtb means somewhere you have not initialized List. - Yura Ivanov