使用SQLITEOpenHelper获取ArrayList不起作用

时间:2012-12-04 18:27:56

标签: java android sqliteopenhelper

我的SQLiteOpenHelper课程有问题。 我有一个打印机制造商的数据库和任何类型的打印机的详细信息。 我尝试使用此代码从我的数据库中获取所有制造商并将其返回到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;
}

我希望每个制造商都在列表中。 不知怎的,我不能让它工作。 我还是新手。

感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

您还可以在SQLite中使用distinct关键字(http://www.sqlite.org/lang_select.html)。使用SQLiteDatabase.rawQuery(String query,String [] args)。

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

答案 1 :(得分:1)

有两个问题:

  1. 一开始,当您的列表manufacturerInList为空时,它不会进入for(String manufacturerInList:manufacturerList){循环内部,因此它永远不会在列表中添加任何条目。

  2. 修复问题1后,仍然无法对if (!manufacturerInList.equals(cursorManufacturer))检查列表中的每个条目,并且可能多次在列表中添加非匹配条目。

  3. 要解决此问题,您有两种选择。

    选项1 :使用contains作为:

                if (!manufacturerList.contains(cursorManufacturer)) {
                     manufacturerList.add(cursorManufacturer); 
                }
    

    Option2 :使用matchFound布尔标志:

            String cursorManufacturer = cursor.getString(0);
            boolean matchFound = false;
    
            //Checking for manufacturer in the list
            for(String manufacturerInList:manufacturerList){
                if (manufacturerInList.equals(cursorManufacturer)){
                     matchFound = true;
                     break;
                }
            }                   
            if(!matchFound){ // <- doesn't exit in the list
                manufacturerList.add(cursorManufacturer); 
            }
    

答案 2 :(得分:0)

使用ArrayList.contains(Object elem)检查ArrayList中是否存在项目。将代码更改为:

  // 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
           if (!manufacturerList.contains(cursorManufacturer)) {
                     manufacturerList.add(cursorManufacturer); 
                } else {
                    System.out.println("cursorManufacturernot found");
                }              
        }while(cursor.moveToNext());
    }