使用 SQLOpenHelper 获取 ArrayList 不起作用



我的SQLiteOpenHelper类有问题。我有一个包含打印机制造商和任何类型打印机详细信息的数据库。我尝试使用此代码从我的数据库中获取所有制造商,并在数组列表中返回它们。

// 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;
}

我希望每个制造商都出现在一个列表中。不知何故,我无法让它工作。我还是个新手。

感谢您的任何帮助。

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

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

有两个问题:

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

  2. 解决问题 1 后,它仍然不起作用,因为if (!manufacturerInList.equals(cursorManufacturer))会检查列表中的每个条目,并可能在列表中多次添加不匹配的条目。

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

选项 1:将contains用作:

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

选项 2:使用 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); 
        }

使用 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());
    }

最新更新