title: android myappadaptor filter for listview
body: <p>Hi i have tried to implement the following code to my listview item. <a href="https://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/" rel="nofollow noreferrer">https://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/</a> , but i cannot get the getfilter to resolve. Mostlikely the myappadaptor doesn't have a getfilter method.</p>
  
  <p>Am i going to correct way about this or should i be taking a different approach.</p>
  
  <p>Thanks</p>
  
  <pre><code>inputSearch.addTextChangedListener(new TextWatcher() {
  
    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
      // When user changed the Text
      ScaleSelect.this.myAppAdapter.getFilter().filter(cs);
    }
  
    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }
  
    @Override
    public void afterTextChanged(Editable arg0) {   
    }
  });
  </code></pre>
  
  <p>Here is my code so far</p>
  
  <pre><code>&gt; package com.dmh.trent.scale_checker;
  &gt; 
  &gt; import android.annotation.SuppressLint; import
  &gt; android.app.ProgressDialog; import android.content.Context; import
  &gt; android.content.Intent; import android.os.AsyncTask; import
  &gt; android.support.v7.app.AppCompatActivity; import android.os.Bundle;
  &gt; import android.view.LayoutInflater; import android.view.View; import
  &gt; android.view.ViewGroup; import android.widget.BaseAdapter; import
  &gt; android.widget.EditText; import android.widget.ListView; import
  &gt; android.widget.TextView; import android.widget.Toast; import
  &gt; java.io.PrintWriter; import java.io.StringWriter; import
  &gt; java.io.Writer; import java.sql.Connection; import
  &gt; java.sql.DriverManager; import java.sql.ResultSet; import
  &gt; java.sql.Statement; import java.util.ArrayList; import java.util.List;
  &gt; import android.text.Editable; import android.text.TextWatcher;
  &gt; 
  &gt; 
  &gt; public class ScaleSelect extends AppCompatActivity {
  &gt; 
  &gt;     private ArrayList&lt;ClassListItems&gt; itemArrayList;  //List items Array
  &gt;     private MyAppAdapter myAppAdapter; //Array Adapter
  &gt;     private ListView listView; // ListView
  &gt;     private boolean success = false; // boolean
  &gt; 
  &gt;     @Override
  &gt;     protected void onCreate(Bundle savedInstanceState) {
  &gt;         super.onCreate(savedInstanceState);
  &gt;         setContentView(R.layout.scale_select);
  &gt;         listView = findViewById(R.id.listView); //ListView Declaration
  &gt;         itemArrayList = new ArrayList&lt;&gt;(); // Arraylist Initialization
  &gt;         EditText inputSearch = findViewById(R.id.sitefiltertext);
  &gt; 
  &gt;         // Calling Async Task
  &gt;         SyncData orderData = new SyncData();
  &gt;         orderData.execute("");
  &gt; 
  &gt; 
  &gt;         inputSearch.addTextChangedListener(new TextWatcher() {
  &gt; 
  &gt;             @Override
  &gt;             public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
  &gt;                 // When user changed the Text
  &gt;                 ScaleSelect.this.myAppAdapter.getFilter().filter(cs);
  &gt;             }
  &gt; 
  &gt;             @Override
  &gt;             public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
  &gt;                                           int arg3) {
  &gt; 
  &gt; 
  &gt;             }
  &gt; 
  &gt;             @Override
  &gt;             public void afterTextChanged(Editable arg0) {
  &gt; 
  &gt;             }
  &gt;         });
  &gt; 
  &gt;     }
  &gt; 
  &gt;     // Async Task has three overrided methods,
  &gt;     @SuppressLint("StaticFieldLeak")
  &gt;     private class SyncData extends AsyncTask&lt;String, String, String&gt; {
  &gt;         String msg = "Internet/DB_Credentials/Windows_FireWall_TurnOn Error, See Android Monitor in the bottom For details!";
  &gt;         ProgressDialog progress;
  &gt; 
  &gt;         @Override
  &gt;         protected void onPreExecute() //Starts the progress dailog
  &gt;         {
  &gt;             progress = ProgressDialog.show(ScaleSelect.this, "Synchronising",
  &gt;                     "ListView Loading! Please Wait...", true);
  &gt;         }
  &gt; 
  &gt;         @Override
  &gt;         protected String doInBackground(String... strings)  // Connect to the database, write query and add items to array list
  &gt;         {
  &gt;             try {
  &gt;                 Class.forName("com.mysql.jdbc.Driver");
  &gt;                 Connection conn = DriverManager.getConnection(Appconfig.connectionString,
  &gt; Appconfig.db_user, Appconfig.db_pass); //Connection Object
  &gt;                 if (conn == null) {
  &gt;                     success = false;
  &gt;                 } else {
  &gt;                     // Change below query according to your own database.
  &gt;                     String query = "SELECT companyname, companystreetname FROM site";
  &gt;                     Statement stmt = conn.createStatement();
  &gt;                     ResultSet rs = stmt.executeQuery(query);
  &gt;                     if (rs != null) // if resultset not null, I add items to itemArraylist using class created
  &gt;                     {
  &gt;                         while (rs.next()) {
  &gt;                             try {
  &gt;                                 itemArrayList.add(new ClassListItems(rs.getString("companyname")));
  &gt;                             } catch (Exception ex) {
  &gt;                                 ex.printStackTrace();
  &gt;                             }
  &gt;                         }
  &gt;                         msg = "Found";
  &gt;                         success = true;
  &gt;                     } else {
  &gt;                         msg = "No Data found!";
  &gt;                         success = false;
  &gt;                     }
  &gt;                 }
  &gt;             } catch (Exception e) {
  &gt;                 e.printStackTrace();
  &gt;                 Writer writer = new StringWriter();
  &gt;                 e.printStackTrace(new PrintWriter(writer));
  &gt;                 msg = writer.toString();
  &gt;                 success = false;
  &gt;             }
  &gt;             return msg;
  &gt;         }
  &gt; 
  &gt;         @Override
  &gt;         protected void onPostExecute(String msg) // disimissing progress dialoge, showing error and setting up my ListView
  &gt;         {
  &gt;             progress.dismiss();
  &gt;             Toast.makeText(ScaleSelect.this, msg + "", Toast.LENGTH_LONG).show();
  &gt;             if (!success) {
  &gt;             } else {
  &gt;                 try {
  &gt;                     myAppAdapter = new MyAppAdapter(itemArrayList, ScaleSelect.this);
  &gt;                     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  &gt;                     listView.setAdapter(myAppAdapter);
  &gt;                 } catch (Exception ignored) {
  &gt; 
  &gt;                 }
  &gt; 
  &gt;             }
  &gt;         }
  &gt;     }
  &gt; 
  &gt;     public class MyAppAdapter extends BaseAdapter         //has a class viewholder which holds
  &gt;     {
  &gt;         class ViewHolder {
  &gt;             TextView textName;
  &gt;         }
  &gt; 
  &gt;         List&lt;ClassListItems&gt; parkingList;
  &gt; 
  &gt;         Context context;
  &gt;         ArrayList&lt;ClassListItems&gt; arraylist;
  &gt; 
  &gt;         private MyAppAdapter(List&lt;ClassListItems&gt; apps, Context context) {
  &gt;             this.parkingList = apps;
  &gt;             this.context = context;
  &gt;             arraylist = new ArrayList&lt;&gt;();
  &gt;             arraylist.addAll(parkingList);
  &gt;         }
  &gt; 
  &gt;         @Override
  &gt;         public int getCount() {
  &gt;             return parkingList.size();
  &gt;         }
  &gt; 
  &gt;         @Override
  &gt;         public Object getItem(int position) {
  &gt;             return position;
  &gt;         }
  &gt; 
  &gt;         @Override
  &gt;         public long getItemId(int position) {
  &gt;             return position;
  &gt;         }
  &gt; 
  &gt;         @SuppressLint("SetTextI18n")
  &gt;         @Override
  &gt;         public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
  &gt;         {
  &gt; 
  &gt;             View rowView = convertView;
  &gt;             ViewHolder viewHolder = null;
  &gt;             if (rowView == null) {
  &gt;                 LayoutInflater inflater = getLayoutInflater();
  &gt;                 rowView = inflater.inflate(R.layout.list_content, parent, false);
  &gt;                 viewHolder = new ViewHolder();
  &gt;                 viewHolder.textName = rowView.findViewById(R.id.textName);
  &gt;                 rowView.setTag(viewHolder);
  &gt;             } else {
  &gt;                 viewHolder = (ViewHolder) convertView.getTag();
  &gt;             }
  &gt;             // here setting up names and images
  &gt;             viewHolder.textName.setText(parkingList.get(position).getName() + "");
  &gt; 
  &gt;             return rowView;
  &gt;         }
  &gt;     }
  </code></pre>
  
  <p>and my xml</p>
  
  <pre><code>&gt; &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &gt; &lt;android.support.constraint.ConstraintLayout
  &gt; xmlns:android="http://schemas.android.com/apk/res/android"
  &gt;     xmlns:app="http://schemas.android.com/apk/res-auto"
  &gt;     xmlns:tools="http://schemas.android.com/tools"
  &gt;     android:layout_width="match_parent"
  &gt;     android:layout_height="match_parent"
  &gt;     tools:context="com.dmh.trent.scale_checker.ScaleSelect"&gt;
  &gt; 
  &gt;     &lt;Button
  &gt;         android:id="@+id/mainmenubuttun"
  &gt;         android:layout_width="200dp"
  &gt;         android:layout_height="wrap_content"
  &gt;         android:layout_marginBottom="40dp"
  &gt;         android:layout_marginEnd="200dp"
  &gt;         android:onClick="mainmenubutton"
  &gt;         android:text="@string/mainmenu"
  &gt;         app:layout_constraintBottom_toBottomOf="parent"
  &gt;         app:layout_constraintRight_toRightOf="parent" /&gt;
  &gt; 
  &gt;     &lt;ListView
  &gt;         android:id="@+id/listView"
  &gt;         android:layout_width="290dp"
  &gt;         android:layout_height="241dp"
  &gt;         android:layout_marginStart="44dp"
  &gt;         android:layout_marginTop="40dp"
  &gt;         android:scrollbars="vertical"
  &gt;         app:layout_constraintStart_toStartOf="parent"
  &gt;         app:layout_constraintTop_toBottomOf="@+id/sitefiltertext" /&gt;
  &gt; 
  &gt;     &lt;EditText
  &gt;         android:id="@+id/sitefiltertext"
  &gt;         android:layout_width="289dp"
  &gt;         android:layout_height="wrap_content"
  &gt;         android:layout_marginStart="44dp"
  &gt;         android:layout_marginTop="40dp"
  &gt;         android:ems="10"
  &gt;         android:hint="@string/Search"
  &gt;         android:inputType="textPersonName"
  &gt;         app:layout_constraintStart_toStartOf="parent"
  &gt;         app:layout_constraintTop_toTopOf="parent" /&gt; &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>