2012-07-27 112 views
1

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

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

回答

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订购您的结果。

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

+0

戴维斯的答案是正确的,但一个额外的“ null“是在Colunm_name ...之前需要的”,而不是null,“Column_name ...”将在HAVING下而不是ORDER BY下。 – sean 2012-07-29 19:10:48

0

你也可以在你的sqlite查询中编辑WHERE子句。 你会希望添加这样的事情

ORDER BY TITLE COLLATE NOCASE ASC 

注“TITLE”是你要排序的列什么。

这会将所有内容放在ABC顺序中,而忽略大小写。 在AaBcD而不是ABDac

得到的使用SQLite你不需要惹所有:)

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);