Search bar with SQLite Databasee

  • Replies:6
  • OpenNot stickiedUnanswered
  • Forum posts: 4

May 7, 2017 10:25:47 AM via Website

Hello, I am very much in need of help. I created a database using SQLite and want to access the data of database using a search interface. So how do i go about it for making the search bar to pull data from SQLite database.
Thanks in Advance.

  • Forum posts: 4

May 8, 2017 6:15:47 AM via Website

Yeah, but the thing is that we need to create method/class which can pull data from sq-lite database and show in search bar as I click and search. As per my research it says it needs some Cursor adapter but I am not able to go on how to do it.

  • Forum posts: 4

May 8, 2017 11:07:08 AM via Website

Thank you very much. This is code.. So the point where I have commented gives error (unfortunately app stops), when the comment is removed.

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
ListView lv;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.ListView2);
myDb = new DatabaseHelper(this);
db=openOrCreateDatabase("BSR_DB",MODE_PRIVATE,null);

    //show item name on ListView

/* Cursor c = db.rawQuery("select Name from bsr_civil",null);
ArrayList ar = new ArrayList();
while(c.moveToNext())
{
ar.add(c.getString(0)); //Some kind of error from here
}

    ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ar);   */
    lv.setAdapter(ad);                                                                
    // select item and show data of selected item on Alert Dialog
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {                   
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           Cursor cr=db.rawQuery("SELECT * FROM bsr_civil WHERE NAME='"+lv.getItemAtPosition(position).toString()+"'",null);
            StringBuffer sb = new StringBuffer();
            while(cr.moveToNext())
            {
                sb.append(cr.getString(0)+"\n");
                sb.append(cr.getString(1)+"\n");
                sb.append(cr.getString(2)+"\n");
                sb.append(cr.getString(3)+"\n");
                sb.append(cr.getString(4)+"\n");
                sb.append(cr.getString(5)+"\n");
                sb.append(cr.getString(6)+"\n");
                sb.append(cr.getString(7)+"\n");
                sb.append(cr.getString(8)+"\n");
            }
            AlertDialog.Builder al = new AlertDialog.Builder(MainActivity.this);     
            al.setTitle("Civil Items");
            al.setMessage(sb.toString());
            al.show();

        }
    });
  • Forum posts: 4

May 20, 2017 2:48:01 AM via Website

Thank You very much, i am getting into an conclusion. So now am unable to pass multiple data to the display interface. I can pass only two. So how do I pass multiple data from sqlite databasee.

 public EntryObject getEntryById(int entryId){

    EntryObject entryObject = null;
    String query = "select * from civil where _id = " + entryId;
    Cursor cursor = this.getDbConnection().rawQuery(query, null);
    if(cursor.moveToFirst()){
        do{
            String code = cursor.getString(cursor.getColumnIndexOrThrow("CODE"));
            String name = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));

            entryObject = new EntryObject(code, name);

        }while(cursor.moveToNext());
    }
    cursor.close();
    return entryObject;

}

Thanks in Advance.