0

Thanks for your time in advance. I'm new to android and I'd like to learn more. I have a code that is displaying data from sql only after search is clicked and I don't know how can I make it display all the data from sql with let's say alfabetical order at first and later on if search is used it will display data matching to search request.

public class EmployeeList extends ListActivity {

    protected EditText searchText;
    protected SQLiteDatabase db;
    protected Cursor cursor;
    protected ListAdapter adapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        searchText = (EditText) findViewById(R.id.searchText);
        db = (new DatabaseHelper(this)).getWritableDatabase();

    }


    public void onListItemClick(ListView parent, View view, int position, long id) {
        Intent intent = new Intent(this, EmployeeDetails.class);
        Cursor cursor = (Cursor) adapter.getItem(position);
        intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
        startActivity(intent);
    }

    public void search(View view) {
        // || is the concatenation operation in SQLite
        cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", 
                        new String[]{"%" + searchText.getText().toString() + "%"});
        adapter = new SimpleCursorAdapter(
                this, 
                R.layout.employee_list_item, 
                cursor, 
                new String[] {"firstName", "lastName", "title"}, 
                new int[] {R.id.firstName, R.id.lastName, R.id.title});
        setListAdapter(adapter);
    }
}

Thanks.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Peter
  • 5
  • 5
  • Check : http://stackoverflow.com/questions/23422072/searchview-in-listview-having-a-custom-adapter/23422665#23422665 – Haresh Chhelana Dec 30 '14 at 12:35
  • remove sql where clause and call code in search method to show all the records. like in: "SELECT _id, firstName, lastName, title FROM employee". and append "order by firstName" to sort accordingly. – Karioki Dec 30 '14 at 12:53
  • Thanks. I want them both to work in the same time, displaying all the record and if user hits search giving the results he was searching for, is it possible on one screen on simply easier make on two different? – Peter Dec 30 '14 at 13:30

1 Answers1

0

i see you are using onClick in XML layout , right? any ways, no problem.

add new method populateList() as following

public void populateList(boolean useWhere) {
    // || is the concatenation operation in SQLite
    if(useWhere){
        cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", new String[]{"%" + searchText.getText().toString() + "%"});
    }else{
        cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee");
    }
    adapter = new SimpleCursorAdapter(
            this, 
            R.layout.employee_list_item, 
            cursor, 
            new String[] {"firstName", "lastName", "title"}, 
            new int[] {R.id.firstName, R.id.lastName, R.id.title});
    setListAdapter(adapter);
}

change search() as following:

public void search(View view){
    populateList(true);
}

add a call populateList(false); to onCreate() of the activity.

so oncreate activity, will call search to execuste without WHERE and when c alled from button click, it will execute the WHERE sql

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        searchText = (EditText) findViewById(R.id.searchText);
        db = (new DatabaseHelper(this)).getWritableDatabase();
        populateList(false);
}
Yazan
  • 6,074
  • 1
  • 19
  • 33
  • that's the xml. no onCLick thanks a lot. – Peter Dec 31 '14 at 10:16
  • @Peter ignore the onClick issue, just try this sol and tell what you got... though i still doubt it's exist, maybe in `@layout/search` as it's included in your XML ` – Yazan Dec 31 '14 at 10:19