获取DataBase中某些项目的重复条目!

时间:2011-07-28 12:22:48

标签: android database sqlite

我正在开发一个Android应用程序。其中我有slideShows。我通过xml解析这些,并在解析它们之后保存在SQLite DB中。大多数幻灯片都是正确保存的,但有时会发生幻灯片被保存两次,即幻灯片中的每张幻灯片都会以不同的PK但相同的内容显然保存两次。这应该避免。

部分代码在这里,我将获取幻灯片并尝试将它们存储在数据库中。

      ArrayList<SlideShowItem> slideItems = null;

      slideItems=Utils.database.getSlideItemOfUrl(Constants.StoriesTable,tempSlideShow.getFullStoryUrl().substring(0, index - 1), type);

                        if (slideItems == null) {
                            Log.d("store in DB: ", " when SlideItems == null ");

                            Log.d("SlideShow Title:   ", tempSlideShow.getTitle());
                            Log.d("SlideShow pub Date:   ", tempSlideShow.getPubDate());

                            slideItems = tempSlideShow.getSlideShow();
                            Utils.database.storeSlideItem(Constants.StoriesTable, myUrl,slideItems, type);
                            Utils.topStorySlidesArrayList = slideItems;
                            slideItems = null ;

                        } else {
                            Log.d("SlideShow Title:   ", tempSlideShow.getTitle());
                            Utils.topStorySlidesArrayList = slideItems;
                            slideItems = null ;
                        } 

和DataBase中函数storeSlideItem的代码是:

 public synchronized void storeSlideItem(String tableName, String, url,ArrayList<SlideShowItem> list, String type) {

    System.out.println("size of the Array list:   " + list.size());

    String newType = null;
    if (type == null) {
        newType = "List";
    }else{
        newType = type;
    }   

    ArrayList<SlideShowItem> newList = new ArrayList<SlideShowItem>();
    //newList = null;
    Iterator<SlideShowItem> iterator = list.iterator();

    while (iterator.hasNext())
    {
        SlideShowItem sSItem = iterator.next();
        if(!newList.contains(sSItem))
        {
            newList.add(sSItem);
        }  
    }

    try {
        for (int i = 0; i < newList.size(); i++) {
            SlideShowItem item = newList.get(i);
            String itemUrl = url + i;// Unique URL for the DB;
            String imgString = null;

            Log.e("Loop Counter", " time " + i);

            Drawable drawable = item.getImage();
            if (item.getBody() != null) {
                item.setBody(item.getBody().replace('\'', '`'));
                // replace as it create syntax error for storing data
            }
            if (item.getSubTitle() != null) {
                item.setSubTitle(item.getSubTitle().replace('\'', '`'));
            }

            if (drawable != null) {
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);

                byte[] b = baos.toByteArray();
                imgString = Base64.encodeBytes(b);
            }

            if (isOpen()) {
                myDB.execSQL("INSERT INTO " + tableName + "(" + column[1] + "," + column[2] + "," + column[3] + "," + column[4] + "," + column[6]
                        + "," + column[7] + ",type) VALUES('" + itemUrl + "','" + item.getSubTitle() + "','" + item.getBody() + "','"
                        + item.getImagePath() + "','" + item.getIndex() + "','" + imgString  + "','" + newType + "Slide')");


                if (item.getBody() != null) {
                    item.setBody(item.getBody().replace('`', '\''));// " ' "
                    // replace as it create syntax error for storing data
                }
                if (item.getSubTitle() != null) {
                    item.setSubTitle(item.getSubTitle().replace('`', '\''));
                }

                if (tableName.equals(Constants.StoriesTable)) {
                    item.setItemId(getItemID(tableName, itemUrl));
                    Utils.hashListStoriesIds.put(itemUrl, item.getItemId());

                    if (imgString != null) {
                        Utils.hashListImages.put(item.getItemId(), new Boolean(true));
                    } else {
                        Utils.hashListImages.put(item.getItemId(), new Boolean(false));
                    }
                }
            }   
        }
    } catch (Exception e) {
        Log.e("Error", "Exception: storeSlideItem type " + e.toString());
    } finally {
        closeConnection();
    }

}

请告诉我任何可以让我摆脱这个恼人问题的事情。任何帮助表示赞赏。

在数据库中复制幻灯片,视图有点像:

1 abc USA 111

2 abc USA 111

等等,这是幻灯片的一张幻灯片。如果我在幻灯片中有3张幻灯片,我将在数据库中获得6个条目,每个幻灯片被保存两次。

1 个答案:

答案 0 :(得分:0)

使用HashSet代替ArrayList