Android Studio SQLite Search From Searchview In Actionbar

Tan Woon How

He is a learner and know little around .Net and Java. He learned and share if you see his mistake... feel free to comment and laugh at him... and also please help to correct him.

You may also like...

  • Shahbaz Ansari

    Hi Tan. I want to store more info like name,middle name,last name,address,etc… and even when i should click the list item it should be displayed in different activity how shall i achieve this implementation?
    And Even i want to update my data to all devices
    so can i use firebase to update my data in all devices?
    if yes then how?
    Please help me i m new to android

    • Tan Woon How

      HI Shahbaz,

      1. Refer to this tutorial for you to go to different activity http://instinctcoder.com/android-studio-sqlite-database-example/
      2. Then you data have to update to server and then from you device you need to have a function to get all data down to the device, you could refer to this for client server communication http://instinctcoder.com/how-to-call-asp-net-web-api-from-android-studio/
      3. Yes, you could you firebase, but i never use that before you have to study it. Unless you are find with the sql server version then the 2nd link is good for you to move on

      Hope that helps
      Good luck

      • David Muro

        Hello Tan, thanks for having done these tutorials, they have been very useful.
        The question of the friend above, is also my doubt, but it is not in that indicated tutorial. We have a database and several activities with EditText to be filled and stored in this database. I tried, before moving to another activity, save the data in the database, but I am not able to continue the recording or update in the third activity.
        Thank you again!

        • Tan Woon How

          Well you have to show me some of your work. From you just said, i cant imagine what’s goes wrong. 🙂

          • David Muro

            Ok… take a look to this partial code, I put this in a third activity, with just one more editText, just for test purpose.
            But the value in this editProntuario is not stored in DB.

            Intent intent = getIntent();
            _Student_Id = intent.getIntExtra(“student_Id”, 0);

            StudentRepo repo = new StudentRepo(this);
            Student student = new Student();
            student = repo.getStudentById(_Student_Id);
            editProntuario.setText(student.prontuario);
            Toast.makeText(this,String.valueOf(student.nome),Toast.LENGTH_SHORT).show();
            }
            public void onClick (View view){
            StudentRepo repo = new StudentRepo(this);
            Student student = new Student();
            student.prontuario= editProntuario.getText().toString();
            student.student_ID=_Student_Id;
            repo.update(student);

            //Pass the id to next activity
            if (view == findViewById(R.id.btnAvancar)){
            Intent objIndent = new Intent(getApplicationContext(),act_tela04.class);
            objIndent.putExtra(“paciente_Id”, Integer.parseInt(String.valueOf(paciente.paciente_ID)));
            startActivity(objIndent);
            }

          • David Muro

            My intention, is make a app, just like a wizard, colecting data step by step, so in the final screen insert/update the data on database.

          • Tan Woon How

            Hi
            Did you see any data on this line student.prontuario= editProntuario.getText().toString();? Before passing in to the repo.update(student) , try to check the value before insert just to make sure there is value. Either use Logcat.Debug or Toast whichever suit you, but Logcat is preferred method you should follow.

            and also could you show me your update (repo.update(student)) function?

            and why are you doing this?
            objIndent.putExtra(“paciente_Id”, Integer.parseInt(String.valueOf(paciente.paciente_ID)));

            I think this should be sufficient?
            objIndent.putExtra(“paciente_Id”, Integer.parseInt(paciente.paciente_ID));

    • David Muro

      I tried to pass the ID to the other activity and work with the same row, searching it again (repo.getStudentById (_Student_Id)) and setting the next editText’s. But something is wrong. 🙁

  • Mateusz Tadeusz Amadeusz Hercu

    Hi Tan. I’m still having a problem with sqlquery : “android.database.sqlite.SQLiteException: no such column: name (code 1): , while compiling: SELECT rowid as _id, id, name, nationality, profession, opus, century, details FROM Artists”. Do you know what’s wrong here?

  • Mateusz Tadeusz Amadeusz Hercu

    Hi Tan. I’m doing similiar part of code in my app and I have errors in lines 26, 28-30 of CustomAdapter.java – ‘item’, ‘txtId’ etc. – these symbols cannot be resolved. Do you have any idea how to solve it? I really appreciate your help.

    • Tan Woon How

      Hi
      It’s mean you did not declare your control on the layout that you inflate . Make sure you have such control on the xml (interface) that you inflate.

  • makkhay

    Hey there, i’m trying to create my project where I’m saving my offline database file( assets folder) with word and definition. When I type a word, it should display the definition. I’m having trouble with making search query. I was going through your code, and was wondering if your search query function would work. Thanks a lot.

    • Tan Woon How

      What error you are getting ? Yes mine 100% work. Showed you the video…hehe

      • makkhay

        I’m not trying to say there is an error, I’m asking you how can I make a search query from my offline pre-populated database file( it is saved in assets folder)?

        • Tan Woon How

          Asset folder? Hmmm….databse usually has predefined location not in asset folder. Im not tried before to save it in asset folder. And the search query on offline db already showed on above example . Take a look on studentRepo.java file. Am i understand you correctly ?

  • חנה קאהן

    maybe you can post a tutorial about identifying a user in android studio?? please!!!

    • Tan Woon How

      Thanks for coming back!!!
      For your request, if it is offline, you could just use this example to query on the sqlite will be fine. and if it is online, i think you could refer to “How to call ASP.Net Web API from Android Studio?” as mentioned in below comment.

      • חנה קאהן

        but I dont have sqlite I use the I get the data for my listview through asp.Net web Api from database(sql server)
        and now I want to search in the listview that’s filled with objects(name and id-hidden) just like you madewith the listview of students in How to call ASP.Net Web API from Android Studio?”
        but I want the search to be offline just with the data in the listview

        • Tan Woon How

          The concept is still the same. You just change it to get data from sqlite and fill it into your POJO or class and then pass it to your adapter.

          • חנה קאהן

            but your customAdapter in that tutorial(How to call ASP.Net Web API from Android Studio?) extends ArrayAdapter and how can I change to get data from sqlite while I’m using retrofit to get data from we api?

          • Tan Woon How

            You make me confuse.

            Just try to understand your need.
            1. Your data will be getting from server and stored it in SQLIte
            2. In your search screen, the searching is perform on SQLite database.

            Is that what you want?

  • חנה קאהן

    Hi, is this search good for the code you posted on connecting webApi from android?

    • Tan Woon How

      You mean you want to perform online search? Yes you could xhange it ti get data from server

      • חנה קאהן

        I’m using your code of “How to call ASP.Net Web API from Android Studio?” so I have a listview that’s filled with objects I got from the server.
        I want to do search in that listview.

        • Tan Woon How

          So have you completed?

  • charandelodev

    Hello, thanks for the tutorial, to fix error “Cannot resolve symbol abc_ic_search_api_mtrl_alpha” (API 24, android studio 2.2) in options_menu.xml change line: android:icon=”@drawable/abc_ic_search_api_mtrl_alpha” with android:icon=”@android:drawable/ic_menu_search”

    if you have error compile with compileSdkVersion , update your SearchWidgetappbuild.gradle with a API compileSdkVersion lasted version. check a new project property and copy/paste code.

    thanks again.

    • Tan Woon How

      Hi that is the icon in the drawable folder. You could get it from the project in download link. Sorry, i forgot where i got that icon from.

      But anyway you could use the android default icon also. Cheers.

      • charandelodev

        thanks for response, i fix with this line android:icon=”@android:drawable/ic_menu_search” , i donwload project, but when run app , debug show error.

        thanks again.

  • Non-Poppin Jid

    Hi bro ^_^
    Thanks to much for this tutorial .
    I want a another tutorial use okHttp library to search from searchview in actionbar can you tutorial this.
    Thanks to much bro ^^

    • Tan Woon How

      Errh.. you need it for getting data from server? Hmmm… I recommend you Retrofit 2.0. I like it because of the simplicity and easy to use and of course the robust of the library.

      When can i make it?Err….Let’s see 🙂

  • Neeraja

    Hi, you presented it very well.
    I’m a beginner to android app development. My customadapter class needs to extend ArrayAdapter. So, I can’t use swapCursor method. What can I do as an alternative solution?
    Thank you.

  • Robert

    HI Sir,
    I was wondering if the searchbar could be created to filter SQL database information inside. And how would one go about implementing the searchbar? I have my list view inside my app and everything works, right now I want to create a search bar and when i try to implement searchwidget and referencing your codes, my app has crashed.
    Was wondering if you have any advice for me(or possibly help me to troubleshoot)

    • Tan Woon How

      Hi Robert,
      Could you paste your error here? We need more information in order to help

      • Robert

        Hi Sir,
        I have removed and recreated my project. It is now in the working state without the Search widget.Hence there is no error. I wanted to know if i wanted to create a search bar in my app(that has a list view already) ,how would i go about doing it? The database is created in phpmyadmin + MySQL.

        If you like, I could also send you my codes and we could work from there?

        Thanks!

        • Tan Woon How

          Please try this again with above tutorial you should be able to do it. Try implement step by step and try it before copy everything into your project.

          Good Luck.

  • Ngawang Tenzin

    hii ..sir how can i add new activity and view those search widget with student detail on this new activity????

    • Tan Woon How

      You can refer to this instinctcoder.com/android-studio-sqlite-database-example/, this example show you how to display student detail in another new activity 🙂

  • Jovi

    Hi, I tried typing “J” but shows 1 record when it should be 4 records. What am I missing?
    Shows “Jimmy Tan Yao Lin” only. Appreciate feedback. TIA

    • Jovi

      Hi,

      I made it right by changing ScrollView to RelativeLayout.

      Thanks anyways.

      • Tan Woon How

        Cool, you make it work!

  • Robert

    Hi How do I add in new Student names? I tried typing it in MainActivity.java but it wont show on the app
    Thanks!

    • Tan Woon How

      Hi
      I assume you mean to add new student in this function insertDummy

      Just add 1 more block after line 88

      studentRepo = new StudentRepo(this);
      student.age=21;
      student.email=”[email protected]”;
      student.name=”Robert Pattinson”;
      studentRepo.insert(student);

      And please take note this is just a dummy function for this tutorial sake. You should have a screen to input this record

      • Qiying

        Hi I followed your instructions but i cannot change or add names inside the student repo.
        And how do i make a screen to input this record?

      • Robert

        Hi
        I have followed the instructions above but I still could not add/delete any student.
        Also, may I know what do you mean to have a screen to input the record?
        Thanks!

        • Tan Woon How

          That’s strange! You have the exact code from this tutorial? It should added for you if you run above code. Did you see any error, in your code do you call this function “insertDummy”?

          I mean you should add 1 more activity(screen) to allow user to input data to save it into database. For example, take a look on this tutorial instinctcoder.com/android-studio-sqlite-database-example/

          • Robert

            I have the exact code. I need to delete and reinstall the app in order to see new changes.No errors in function insertDummy,everything looks ok so Im puzzled as well.

            Im trying to create a database that only I can edit, so other people can only view. Will not including this page(input data) cause any problems?

            Thanks

          • Tan Woon How

            Arhh.. ok my bad. in the onCreate function at line 41, there is a checking to make sure no duplicate data after you run the application for multiple times, so if you like to delete and reinsert again, do the following

            //Write you delete function 🙂 you could refer to this tutorial http://instinctcoder.com/android-studio-sqlite-database-example/
            insertDummy();

  • Qiying

    Hello Sir. Thank you for the tutorial. We are new to android studio programming and we have encountered an issue in the programming.
    The codes are all working with no error except that there is no searchbar that is displayed above the page. Is there anyway that you could help us?

    • Tan Woon How

      Hmmm….are you able to run the original code from your machine ? Beside what theme did you you configured in your project ?

      • Qiying

        We are able to run our codes before the implementation of this tutorial.However, I am unable to run this tutorial codes in a seperate project file. It is without the searchbar as well.
        our styles theme is as follows

        • Tan Woon How

          Hmm..Not sure what is going on but if you don’t mind to send your code to my email admin[a]instinctcoder.com, and see what i can help.

          • Qiying

            Thank you Mr Tan!
            Our codes work now.
            Much appreciation!

        • Tan Woon How

          To who has facing the same problem as Qiying,
          If you like to use this tutorial’s code, please make sure you
          1) import android.support.v7.widget.SearchView; instead of android.widget.SearchView
          2) Extends AppCompatActivity instead of Activity.

  • geekE

    Thank you for the tutorial, I try to use an existing sqlite database but it’s not working. Any advice ? Thank you.

    • Tan Woon How

      Hi
      Try to debug and see any result returned from your query. To be frank it could be anything if without seeing your code ☺

      • geekE

        Yes, I know… I only have the first student that appears but nothing else. And my database is a .sqlite2. Do you think it can be the problem ?

        • Tan Woon How

          I dont think is cause by that. Since you have data returned means sqlite2 is not the issue. Check your query

  • David

    okay, i came here, really helpful. thanks. i added search functionality to the activity. but now how do i make the items clickable, i’d like to be able to open new activities with details of the clicked item on the listview

    • Tan Woon How

      Actually, Is the same way you did before. Meaning add these lines in the onCreate function after line 39

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Overridepublic void onItemClick(AdapterView parent, View view, int position, long id) {
      TextView student_Id=(TextView) view.findViewById(R.id.txtId);
      Log.d(TAG, student_Id.getText().toString());
      }
      });

      • David

        you helped me out greatly! Thank You Tan

        • Tan Woon How

          Great finally you did it !!!

  • ricko monty

    Thank you Sir for the useful tutorial, very nice of you to grant a request.