1

I'm making an application that can search a table 'employee' and return results. How may I use an array adapter to do this? I'm new to android.

public class SimpleSearch extends Activity {
    /** Called when the activity is first created. */
    protected SQLiteDatabase db;
    Intent intent = getIntent();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doMySearch(query);
        }
    }
    public List<String> doMySearch(String query) {
        List<String> result = new ArrayList<String>();

        Cursor c = db.query(
                "employee", 
                new String[] { "_id" }, // The column you want as a result of the Query
                "firstName like '%?%' OR lastName like '%?%' OR officePhone like '%?%'", // The where-Clause of the statement with placeholders (?)
                new String[] { query, query, query }, // One String for every Placeholder-? in the where-Clause
                );
        while (c.moveToNext()) {
            result.add(c.getString(0));
        }
        c.close();
        return result;
    }


}
1

2 Answers 2

1

You don't have to use array adapter, use SimpleCursorAdapter instead. If you need an example on using it, you can have a look at the Notepad tutorial.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use the ListActivity or a ListFragment and Populate them using an CursorAdapter.(SimpleCursorAdapter) is an implementation class

CursorAdapter can be populated from the List returned by your doMySearchMethod.

1 Comment

something like this? public void doMySearch(String query) { cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", new String[]{"%" + query.getText().toString() + "%"}); adapter = new SimpleCursorAdapter( this, R.layout.emp_list_item, cursor, new String[] {"firstName", "lastName", "title"}, new int[] {R.id.firstName, R.id.lastName, R.id.title}); setListAdapter(adapter); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.