按字母顺序(从数据库)对游标中的内容排序列表视图

时间:2013-04-10 04:21:26

标签: android sorting listview cursor

我已经在这里阅读了一些关于如何使用例如collections.sort或sortOrder按字母顺序排列列表视图的主题,但我无法在我的情况下使其工作,因为我没有使用arraylist。

我的列表视图中填充了首先放入游标的数据库中的元素,这样:

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 ) : ");
}  

我正在寻找最简单的方法来指定我希望结果按字母顺序显示(name / ID_NAME)。所以我的问题是,有没有办法添加如下行:

  

sortOrder(name,byAlph)

2 个答案:

答案 0 :(得分:5)

查询数据库时,只需添加“ORDER BY ID_NAME”。

或者如果您想使用query builder

答案 1 :(得分:3)

按字母顺序对数据进行排序:

Cursor cursor = getContentResolver.query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");

就像一个魅力