2

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

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

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) 一起按字母顺序显示。所以我的问题是,有没有办法添加一行:

排序顺序(名称,byAlph)

4

2 回答 2

6

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

或者,如果您想使用查询生成器

于 2013-04-10T04:24:49.540 回答
3

要按字母顺序对数据进行排序,请执行以下操作:

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

奇迹般有效

于 2014-01-18T13:05:13.350 回答