Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8118045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:21:43+00:00 2026-06-06T04:21:43+00:00

this program is for class to display the database in Listview.i want to add

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:21:46+00:00Added an answer on June 6, 2026 at 4:21 am
        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));
    }
              }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code like this: Class Program { static StreamReader sr
First look at this code: class Program { static void Main(string[] args) { var
I have simplified my code to this internal class Program { private static void
I want to display images stored in my database,however my program is throwing an
I have this program that I am working on for class, I think the
I am writing a program for class that manages a Hotel. This Report1 function
For context - read this . Problem: class Program { static void Main() {
What is the workaround to this problem? class Program { static void Main(string[] args)
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.