2

this program is for class to display the database in Listview.i want to add search for the required data in database by given keywords.How to add the code for searching in database with keywords in the same class

  public class DisplayActivity extends Activity
  {

private ArrayList<String> arraylist=new ArrayList<String>();
private SQLiteDatabase MYdatabase;
ListView listView;
Button back;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);
    listView=(ListView)findViewById(R.id.listView1);

        DatabaseHelper db=new DatabaseHelper(this);
        MYdatabase=db.getWritableDatabase();
    try {   
        Cursor c=MYdatabase.rawQuery("SELECt * FROM Student", null);
        if(c!=null)
        {
            if(c.moveToFirst())
            {
                do {
                    String name=c.getString(c.getColumnIndex("NAME"));
                    String age=c.getString(c.getColumnIndex("AGE"));
                    String address=c.getString(c.getColumnIndex("ADDRESS"));
                    arraylist.add("Name :"+name+"\n"+"Age :"+age+"\n"+"Address :"+address+"\n");

                } while (c.moveToNext());
            }
        }

        c.close();
        c.deactivate();

    } catch (SQLiteException e) {

        Log.d(getClass().getSimpleName(), "could not create"+"or open the database");

    }
    finally
    {
        if(MYdatabase!=null)
        {
          MYdatabase.close();
        }
    }
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arraylist));
}
public void Back(View v)
{
    Intent back=new Intent(DisplayActivity.this,DbExampleActivity.class);
    startActivity(back);
}
}

the following is the activity class code. thought it ll be easy to understand

 public class DbExampleActivity extends Activity implements OnClickListener{
 Button submit,select,search;
 AlertDialog di;
 private SQLiteDatabase sqLiteDatabase;
 private SQLiteStatement sqLiteStatement;
 private String name,age,address;
 private static final String TABLE_NAME="Student";
@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    submit=(Button)findViewById(R.id.savebutton);
    select=(Button)findViewById(R.id.detailsbutton);
    search=(Button)findViewById(R.id.searchbutton);
    DatabaseHelper db=new DatabaseHelper(this);
    sqLiteDatabase=db.getWritableDatabase();
    sqLiteStatement=sqLiteDatabase.compileStatement("insert into "+TABLE_NAME+"(name,age,address)values(?,?,?)");
   submit.setOnClickListener(this);
   select.setOnClickListener(this);
   search.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.detailsbutton:
        Intent in=new Intent(getApplicationContext(),DisplayActivity.class);
        startActivity(in);

        break;
    case R.id.savebutton:
        name=((EditText)findViewById(R.id.editText1)).getText().toString().trim();
        age=((EditText)findViewById(R.id.editText2)).getText().toString().trim();
        address=((EditText)findViewById(R.id.editText3)).getText().toString().trim();

        sqLiteStatement.bindString(1, name);
        sqLiteStatement.bindString(2, age);
        sqLiteStatement.bindString(3, address);
        sqLiteStatement.executeInsert();

        Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_LONG).show();
        break;
    case R.id.searchbutton:
         Intent search=new Intent(getApplicationContext(),DisplayActivity.class);
         startActivity(search);
    default:
        break;
    }

}
}

What code is to be added to this to allow searching the database using keywords

Dre
  • 4,298
  • 30
  • 39
aravind varma
  • 90
  • 5
  • 13

3 Answers3

2
    EditText1=(EditText)findViewById(R.id.editText1);
    EditText1.addTextChangedListener(new TextWatcher() {

        @Override
                 public void onTextChanged(CharSequence s, int start, int before,int count) 
    {
                     populateListView(s.toString());

                    }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

              public void populateListview(String s){

           DatabaseHelper db = new DatabaseHelper(this);
           MYdatabase = db.getWritableDatabase();

    try {   
    Cursor c=MYdatabase.rawQuery(" SELECT * FROM Student WHERE City ID '"+s+%"'", null); 
    if(c!=null)
    {
        if(c.moveToFirst())
        {
            do {
                String name=c.getString(c.getColumnIndex("NAME"));
                String age=c.getString(c.getColumnIndex("AGE"));
                String address=c.getString(c.getColumnIndex("ADDRESS"));
                arraylist.add("Name :"+name+"\n"+"Age :"+age+"\n"+"Address :"+address+"\n");

            } while (c.moveToNext());
        }
    }

    }

    c.close();
    db.close();
    db=null;

    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arraylist));
}
          }
Vivekanand
  • 755
  • 1
  • 8
  • 29
0

From what i have understood i think you can use Like Clause

eg:

   " SELECT * FROM Student WHERE City LIKE '"+ keyWord +%'"
ranjeet wagh
  • 306
  • 3
  • 2
0

Replace Cursor c=MYdatabase.rawQuery("SELECt * FROM Student", null); with Cursor c=MYdatabase.rawQuery("SELECT * FROM Student WHERE id=" + ID, null);

Retrieve the ID from the EditText using editText.getText().toString(); and pass it as an argument while you are quering the database.

Vivekanand
  • 755
  • 1
  • 8
  • 29