开发者

Get repeated Entries for Some Items in DataBase!

开发者 https://www.devze.com 2023-03-23 18:22 出处:网络
I am working on an android application. In which i haveslideShows. I am parsing these through an xml and after parsing them, saving in the SQLite DB. Majority of the slideshows are saved properly but,

I am working on an android application. In which i have slideShows. I am parsing these through an xml and after parsing them, saving in the SQLite DB. Majority of the slideshows are saved properly but, sometimes this happens that the slides are saved two times that is, every slide in the slideShow is saved two times obviously with different PK but same content. which should be avoided.

Partial code is here, where i am gett开发者_运维问答ing the slides and trying to store them in DB.

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

and code of function storeSlideItem in DataBase is:

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

}

Please tell me anything that can get me out of this irritating problem. Any help is appreciated.

in DB for duplication of slides the view is somewhat like:

1 abc USA 111

2 abc USA 111

and so on this was for one slide of a slideShow. if i have 3 slides in a slideshow, i'll get 6 entries in DB each slide being saved for two times.


Use HashSet instead of ArrayList

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号