title: Search function not working in android studio
body: <p>I'm trying to create a search box that will return all the listings in my database that have the same tag as what the user wrote in the search bar, but for some reason, when I go to the new intent, the list just stays the same, showing every listing that I have in the database. Any help?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>package com.example.shareandcare;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.media.Image;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.AutoCompleteTextView;
  import android.widget.Button;
  import android.widget.CursorAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.lang.reflect.Array;
  
  public class listings extends Activity {
  
      private Cursor c = null;
      private listingHelperAdapter adapter = new listingHelperAdapter(c);
      private listingHelper helper = new listingHelper(this);
      private ListView list;
      private Button listingsAddListingButton, listingsSearchButton;
      private AutoCompleteTextView listingsSearch;
      private TextView empty = null;
      private String searchStr = null;
  
      private static final String[] TAGS = new String[] {
              "Sofa", "Table", "Bed", "Storage", "Tool"
      };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.listings);
  
          empty = (TextView)findViewById(R.id.listings_Empty);
          list = (ListView)findViewById(R.id.listings_List);
  
          listingsAddListingButton = (Button)findViewById(R.id.listings_Add_Listing_Button);
          listingsSearchButton = (Button)findViewById(R.id.listings_Search_Button);
          listingsSearch = (AutoCompleteTextView)findViewById(R.id.listings_Search);
          c = helper.getAll();
          list.setAdapter(adapter);
  
          ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(this,
                  android.R.layout.simple_list_item_1, TAGS);
          listingsSearch.setAdapter(arrayAdapter);
  
          listingsSearchButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (listingsSearch.getText().toString() != "") {
                      searchStr = listingsSearch.getText().toString();
                      Intent intent = new Intent(listings.this, listings.class);
                      intent.putExtra("search", searchStr);
                      helper.setSearchStat(true);
                      //finish();
                      startActivity(intent);
                  } else if (listingsSearch.getText().toString() == null){
                      helper.setSearchStat(false);
                  }
              }
          });
  
          list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                  helper.delete(String.valueOf(l));
                  //helper.resetDatabase();
                  Toast.makeText(listings.this, "Deleted", Toast.LENGTH_SHORT).show();
                  Intent intent = new Intent(listings.this, listings.class);
                  finish();
                  startActivity(intent);
                  return true;
              }
          });
  
          list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                  c.moveToPosition(i);
                  String title = helper.getListingTitle(c);
                  Intent intent = new Intent(listings.this, listing.class);
                  intent.putExtra("title",title);
                  startActivity(intent);
              }
          });
  
          listingsAddListingButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent i = new Intent(listings.this, addListing.class);
                  startActivity(i);
              }
          });
  
      }
  
      @Override
      protected void onResume(){
          super.onResume();
  
          if (c != null) {
              c.close();
          }
          if (helper.getSearchStat() == false) {
              c = helper.getAll();
          } else if (helper.getSearchStat() == true) {
              c = helper.getByTag(getIntent().getStringExtra("search"));
          }
  
  
          if (c.getCount() &gt; 0) {
              empty.setVisibility(View.INVISIBLE);
          }
          adapter.swapCursor(c);
      }
  
      @Override
      protected void onPause(){
          super.onPause();
  
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          helper.close();
  
      }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.options, menu);
          return super.onCreateOptionsMenu(menu);
      }
      @Override
      public boolean onOptionsItemSelected (MenuItem item){
  
          if (item.getItemId()== R.id.log_out) {
              Toast.makeText(this, "Logged out...", Toast.LENGTH_SHORT).show();
              Intent intent;
              intent = new Intent(listings.this, mainActivity.class);
              startActivity(intent);
          }
          return super.onOptionsItemSelected(item);
  
      }
  
      class listingHelperAdapter extends CursorAdapter {
          listingHelperAdapter(Cursor c) {
              super(listings.this, c);
          }
  
          @Override
          public void bindView(View row, Context ctxt, Cursor c) {
              listingHolder holder = (listingHolder) row.getTag();
  
              holder.populate(c, helper);
          }
  
          @Override
          public View newView(Context ctxt, Cursor c, ViewGroup parent) {
              LayoutInflater inflater = getLayoutInflater();
              View row = inflater.inflate(R.layout.listingrow, parent, false);
              listingHolder holder = new listingHolder(row);
  
              row.setTag(holder);
              return (row);
          }
      }
  
      static class listingHolder {
          private TextView listingTitle, listingTag, listingDescription;
          //private ImageView furnitureImage = null;
  
          listingHolder (View row){
              listingTitle = (TextView) row.findViewById(R.id.listing_Title);
              listingTag = (TextView) row.findViewById(R.id.listing_Tag);
              listingDescription = (TextView) row.findViewById(R.id.listing_Description);
          }
  
          void populate (Cursor c,listingHelper helper){
              listingTitle.setText(helper.getListingTitle(c));
              listingTag.setText(helper.getListingTag(c));
              listingDescription.setText(helper.getListingDesciption(c));
          }
      }
  }
  
  </code></pre>
  
  <p>And if it helps, here is my database helper as well:</p>
  
  <pre><code>package com.example.shareandcare;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  
  public class listingHelper extends SQLiteOpenHelper {
  
      private boolean x = false;
  
      public listingHelper(Context context) {
          super(context, "listing.db", null, 2);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          db.execSQL("create table listing(_id integer primary key autoincrement, listingTitle text, listingTag text, listingDescription text, listingDimensionX float, listingDimensionY float, listingDimensionZ float, listingImage text, listingStatus text)");
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
          db.execSQL("drop table if exists listing");
          onCreate(db);
      }
  
      public Cursor getAll() {
          return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing order by listingTag", null));
      }
  
      public Cursor getAllConfirmed() {
          String arg = "1";
          String[] args = {arg};
          return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where listingStatus=?", args));
      }
  
      public Cursor getById(String id) {
          String[] args = {id};
  
          return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, lisitngDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where _id=?", args));
      }
  
      public Cursor getByTitle(String title) {
          String[] args = {title};
  
          return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where listingTitle =?", args));
      }
  
      public Cursor getByTag(String tag) {
          tag = tag.toLowerCase();
          String[] args = {tag};
  
          return (getReadableDatabase().rawQuery("select _id, listingTitle, listingTag, listingDescription, listingDimensionX, listingDimensionY, listingDimensionZ, listingImage from listing where listingTag =?", args));
      }
  
      public void setSearchStat(boolean status) {
          x = status;
      }
  
      public boolean getSearchStat() {
          return x;
      }
  
      public void updateListingStatus(String title) {
          ContentValues cv = new ContentValues();
          String[] args ={title};
  
          cv.put("listingStatus", "1");
          getWritableDatabase().update("listing", cv, "listingTitle=?", args);
      }
  
      public void addListing(String listingTitle, String listingTag, String listingDescription, String listingDimensionX, String listingDimensionY, String listingDimensionZ, String listingImage) {
          ContentValues cv = new ContentValues();
  
          cv.put("listingTitle", listingTitle);
          cv.put("listingTag", listingTag.toLowerCase());
          cv.put("listingDescription", listingDescription);
          cv.put("listingDimensionX", listingDimensionX);
          cv.put("listingDimensionY", listingDimensionY);
          cv.put("listingDimensionZ", listingDimensionZ);
          cv.put("listingImage", listingImage);
  
          getWritableDatabase().insert("listing", "listingTitle", cv);
      }
  
      public void delete(String id) {
          String[] args = {id};
          getWritableDatabase().delete("listing","_id=?", args);
      }
  
      public String getID(Cursor c) {
          return (c.getString(0));
      }
  
      public String getListingTitle (Cursor c) {
          return (c.getString(1));
      }
  
      public String getListingTag (Cursor c) {
          return (c.getString(2));
      }
  
      public String getListingDesciption (Cursor c) {
          return (c.getString(3));
      }
  
      public String getListingDimensionX (Cursor c) {
          return (c.getString(4));
      }
  
      public String getListingDimensionY (Cursor c) {
          return (c.getString(5));
      }
  
      public String getListingDimensionZ (Cursor c) {
          return (c.getString(6));
      }
  
      public String getListingImage (Cursor c) {
          return (c.getString(7));
      }
  
      public void resetDatabase() {
          getWritableDatabase().execSQL("drop table if exists listing");
      }
  }
  
  </code></pre>