Search code examples
androidsortinglistviewcursor

Sort listview with content from cursor alphabetically (from database)


I have read some topics here about how to order listviews alphabetically using for example collections.sort or sortOrder but I can't make it work in my case, as I am not using an arraylist.

My listview is populate with elements from a database first put into a cursor, this way :

private void displayListView() {

    // getExtra
    Bundle bundle = getIntent().getExtras();
    String title = bundle.getString("title", "Choose here :");
    String inInterval = bundle.getString("inInterval");
    Log.d(TAG, "inInterval = " + inInterval);
    poititle.setText(title);
    // put the results of the method in a cursor
    c = dbHelper.findPoiInTable(inInterval);
    displayCursor();
} 

private void displayCursor() {

    String[] columns = new String[] { DatabaseAdapter.COL_NAME,
            DatabaseAdapter.COL_STREET, DatabaseAdapter.COL_WEBSITE,
            DatabaseAdapter.COL_TELEPHONE, DatabaseAdapter.COL_REMARKS,
            DatabaseAdapter.COL_PRICE, DatabaseAdapter.COL_MOBILE };
    int[] to = new int[] { R.id.name, R.id.street, R.id.website,
            R.id.telephone, R.id.remarks, R.id.price, R.id.mobile };
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
            columns, to, 0);

    ListView listView = (ListView) findViewById(R.id.poilistview);
    // Assign adapter to ListView
    listView.setAdapter(cursorAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        // Comportement des éléments de la listview
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent i = new Intent(getApplicationContext(),
                    POIActivity.class);

            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String street = ((TextView) view.findViewById(R.id.street))
                    .getText().toString();
            String website = ((TextView) view.findViewById(R.id.website))
                    .getText().toString();
            String telephone = ((TextView) view
                    .findViewById(R.id.telephone)).getText().toString();
            String remarks = ((TextView) view.findViewById(R.id.remarks))
                    .getText().toString();
            String price = ((TextView) view.findViewById(R.id.price))
                    .getText().toString();
            String mobile = ((TextView) view.findViewById(R.id.mobile))
                    .getText().toString();
            // i.putExtra(ID_EXTRA, name) ;
            i.putExtra(ID_NAME, name);
            i.putExtra(ID_STREET, street);
            i.putExtra(ID_WEBSITE, website);
            i.putExtra(ID_TELEPHONE, telephone);
            i.putExtra(ID_REMARKS, remarks);
            i.putExtra(ID_PRICE, price);
            i.putExtra(ID_MOBILE, mobile);
            startActivity(i);
        }

    });

    int numberResults = listView.getCount();
    listviewCount.setText("( " + numberResults + " results ) : ");
}  

I am looking for the easiest way to specify I want the results to be displayed alphabetically with the word (name / ID_NAME). So my question is, is there a way to add a line like :

sortOrder(name, byAlph)


Solution

  • When you query the database, just add in "ORDER BY ID_NAME".

    Or if you want to use the query builder