Search code examples
androidsqlitecollectionssortingcomparable

ArrayList Natural Ordering


I have a application using a sqlite DB which is put into an ArrayList. I am trying to get the entries to show in alphabetical or from what I understand "natural ordering". I have implemented comparable and have added my collections.sort statement but the array list remains in non alphabetical or natural order. please advise

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

Solution

  • Replace this line:

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

    With this:

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

    To order your results by the COLUMN_NAME_SITE.

    This way, your cursor will be preordered when you have done the query and you don't need a comparable interface.