1

我有一个使用放入 ArrayList 的 sqlite DB 的应用程序。我试图让条目以字母顺序或我理解的“自然顺序”显示。我已经实现了可比较并添加了我的 collections.sort 语句,但数组列表保持非字母顺序或自然顺序。请指教

public class LoginList extends Activity implements OnClickListener, OnItemClickListener {

private ListView loginList;
private Button webLogin;
private ListAdapter loginListAdapter;
private ArrayList<LoginDetails> loginArrayList;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_listview);

    loginList = (ListView)
    findViewById(R.id.loginlist);
    loginList.setOnItemClickListener(this);

    webLogin = (Button)
    findViewById(R.id.button3);
    webLogin.setOnClickListener(this);

    loginArrayList = new ArrayList<xxxxxxxx>();
    loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
    loginList.setAdapter(LAdapter);
    Collections.sort(AList );

    }

@Override
public void onClick (View v) {
    Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
    startActivity(webLoginIntent);

}

public List<String> populateList (){

    List<String> webNameList = new ArrayList<String>();


    dataStore openHelperClass = new dataStore (this);

    SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase("xxxxxxxx");

    Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, null);

    startManagingCursor(cursor);

    while (cursor.moveToNext()){

    String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
    String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
    String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
    String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));

    LoginDetails lpDetails = new LoginDetails();

        lpDetails.setsName(sName);
        lpDetails.setwUrl(wUrl);
        lpDetails.setuName(uName);
        lpDetails.setpWord(pWord);

        loginArrayList.add(lpDetails);
        webNameList.add(sName); 
}
sqliteDatabase.close();
return webNameList;
}

@Override
protected void onResume() {
    super.onResume();
loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
loginList.setAdapter(loginListAdapter);
    }

@Override
public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
    Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();

    Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);

    LoginDetails clickedObject = loginArrayList.get(arg2);

        Bundle loginBundle = new Bundle();
    loginBundle.putString("clickedWebSite",clickedObject.getsName());
    loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
    loginBundle.putString("clickedUserName",clickedObject.getuName());
    loginBundle.putString("clickedPassWord",clickedObject.getpWord());  

    updateDeleteLoginInfo.putExtras(loginBundle);   
    startActivity(updateDeleteLoginInfo);
}
    }

SQLite:

 String sqlDataStore = "create table if not exists " + TABLE_NAME_XXXXXXXXX + " ("+ BaseColumns._ID + "integer primary key autoincrement "

                    + COLUMN_NAME_XXXX + " text not null,"
                    + COLUMN_NAME_XXXXXXX + " text not null,"
                    + COLUMN_NAME_XXXXXXXX + " text not null,"
                    + COLUMN_NAME_XXXXXXXX + " text not null);"
4

3 回答 3

0

替换这一行:

  Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, null);

有了这个:

  Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);

按 COLUMN_NAME_SITE 对结果进行排序。

这样,当您完成查询时,您的光标将被预先排序,并且您不需要可比较的界面。

于 2012-07-27T14:04:26.093 回答
0

您也可以在 sqlite 查询中编辑 WHERE 子句。你会想要添加这样的东西

ORDER BY TITLE COLLATE NOCASE ASC

注意“TITLE”是您要排序的任何列。

这将在忽略大小写的情况下将所有内容按 ABC 顺序排列。导致 AaBcD 而不是 ABDac

使用 SQLite,您根本不需要进行比较:)

于 2012-07-27T14:07:02.600 回答
0
Cursor cursor = db.rawQuery("select * from TABLENAME_HERE order by COLUMN_NAME_HERE", null);

替换而不是

Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, null);
于 2012-07-27T14:09:31.900 回答