3

我的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;
}

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

谢谢你的帮助。

4

3 回答 3

3

您还可以在 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);

于 2012-12-04T18:43:41.793 回答
1

有两个问题:

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

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

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

选项1contains用作:

            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); 
        }
于 2012-12-04T18:45:13.157 回答
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());
    }
于 2012-12-04T18:32:50.607 回答