【问题标题】:ArrayList Natural OrderingArrayList 自然排序
【发布时间】:2012-07-26 05:23:05
【问题描述】:

我有一个使用放入 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);"

【问题讨论】:

    标签: android sqlite collections sorting comparable


    【解决方案1】:

    替换这一行:

      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 对结果排序。

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

    【讨论】:

    • 戴维斯的答案是正确的,但是在没有空值的情况下,在 Colunm_name..." 之前需要一个额外的 "null","Column_name..." 将在 HAVING 下而不是 ORDER BY 下。
    【解决方案2】:

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

    ORDER BY TITLE COLLATE NOCASE ASC
    

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

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

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

    【讨论】:

      【解决方案3】:
      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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 2010-09-07
        相关资源
        最近更新 更多