Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ArrayList with SQLOpenHelper not working

I have a problem with my SQLiteOpenHelper class. I have a database with printer manufacturers and details of any kind of printer. I try to get all manufacturer from my database with this code and returning them in a arraylist.

// Read database for All printer manufacturers and return a list
public ArrayList<String> getPrManufacturer(){
    ArrayList<String> manufacturerList = new ArrayList<String>();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(CoDeskContract.Printer.TABLE_NAME,
                             printerManuProjection, null, null, null, null, null,null);

    // If cursor is not null and manufacturer List 
    // does not contains Manufacturer in the list then add it!
    if ((cursor != null) && (cursor.getCount()>0)){
        cursor.moveToFirst();
        do {
            String cursorManufacturer = cursor.getString(0);
            //Checking for manufacturer in the list
            for(String manufacturerInList:manufacturerList){
                if (!manufacturerInList.equals(cursorManufacturer))
                    manufacturerList.add(cursorManufacturer);               
            }                   
        }while(cursor.moveToNext());
    }

    // Return list of manufacturers from database
    return manufacturerList;
}

I want every manufacturer to be once in a list. Somehow i cant to get it to work. Im still a newbie.

Thanks for any Help.

like image 967
Katamave Avatar asked Dec 30 '25 03:12

Katamave


1 Answers

You can also use the distinct keyword in SQLite (http://www.sqlite.org/lang_select.html). Use SQLiteDatabase.rawQuery(String query, String[] args) for that.

db.rawQuery("SELECT DISTINCT name FROM " + CoDeskContract.Printer.TABLE_NAME,null);