在服装gridview中显示图像时重复的值

时间:2014-01-09 08:32:54

标签: java android

我很难弄清楚为什么我的图像在我的服装网格中显示重复(staggeredgridview)

使用惰性方法从webservice获取图像并插入到db中的程序,如果用户访问没有Internet的程序或访问旧图像数据,则将从本地db中获取。

这是我的主要课程:

private int index;
private Mysecondworker msw;
private main activity;
private String value;
private int filter = 0;
private boolean isLoading = false;
private int Sort = 0;
private StaggeredGridView mystaggerdgridview ;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    mystaggerdgridview = (StaggeredGridView) findViewById(R.id.grid);
    mystaggerdgridview.setColumnCount(2);
    activity = this;
    loadingisactive(true);
    msw = new Mysecondworker(activity, value, String.valueOf(index++), 0,0);
    msw.execute();

    mystaggerdgridview .setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(ViewGroup view, int scrollState) {

        public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            boolean needtofetchMore = firstVisibleItem + visibleItemCount  >= totalItemCount ;
            if (needtofetchMore && !loadingisactive){
                loadingisactive= true;
                hw = new Mysecondworker(activity, value, String.valueOf(index++), filter,Sort);
                hw.execute();
            }

        }
    });
}
    private Thread performOnBackgroundThread(final Runnable runnable) {
    final Thread t = new Thread() {
        @Override
        public void run() {
            try {
                runnable.run();
            } finally {

            }
        }
    };
    t.start();
    return t;
}
public void setloadingisactive(boolean flag){
    loadingisactive= flag;
}

和我的Mysecondworker课程:

public class Mysecondworker 
        extends
        AsyncTask<String, String, AsyncTaskResult<ArrayList<HashMap<String, String>>>> {


    private main  targetCtx;
    private String Mysecondworker_key;
    myAdapter myadapter;
    private LinearLayout Mysecondworker_progress;
    private String Mysecondworker_index , Mysecondworker_sort;
    private int Mysecondworker_filter = 0;
    private LinearLayout Mysecondworker_pagination;
    private ImageView Mysecondworker_imgPagination;
    private AnimationDrawable Mysecondworker_frameAnimation;
    private String Mysecondworker_lastID = "0";
    private StaggeredGridView mystaggerdgridview ;

    public Mysecondworker(main mycontext, String mykey, String myindex, int myfilter,int mysort) {
        this.targetCtx = mycontext;
        Mysecondworker_key = mykey;
        mystaggerdgridview = (StaggeredGridView) targetCtx.findViewById(id.grid);
        Mysecondworker_index = myindex;
        Mysecondworker_filter = myfilter;
        Mysecondworker_sort = String.valueOf(mysort);
        Mysecondworker_pagination = (LinearLayout) targetCtx.findViewById(id.pagination_load);
        Mysecondworker_imgPagination = (ImageView) targetCtx
                .findViewById(id.loadingpaginganimation);


    @Override
    protected void onPreExecute() {
        progress = (LinearLayout) targetCtx.findViewById(R.id.progress);

        if (Integer.parseInt(index) == 0)
            Mysecondworker_progress.setVisibility(View.VISIBLE);
        Mysecondworker_pagination.setVisibility(View.VISIBLE);
        Mysecondworker_imgPagination.setBackgroundResource(R.anim.loadanimation);
        Mysecondworker_frameAnimation = (AnimationDrawable) Mysecondworker_imgPagination.getBackground();
        Mysecondworker_frameAnimation.start();

        //finding the last id of images
        myAdapter mAd = (myAdapter) mystaggerdgridview.getAdapter();
        if (mAd != null && mAd.getCount() > 0) {
            lastID = mAd.getData().get(mAd.getCount() - 1).get("id");
        }else
            lastID = "0";



    }

    @Override
    protected AsyncTaskResult<ArrayList<HashMap<String, String>>> doInBackground(
            String... param) {

        try {
            String xml = [[[getting image from web service with  unique id for each one]]]
            SaxXmlParser mygalleryparser = new SaxXmlParser(xml, "imagegallery");
            mygalleryparser.runParser();

            if (Integer.parseInt(Mysecondworker_index) == 0 && Integer.parseInt(Mysecondworker_key) > 0) {
                table_imagegallery obj_imagegallery = new table_imagegallery();
                obj_imagegallery.subjectId = Integer.parseInt(Mysecondworker_key);
                obj_imagegallery.DeleteBySubjectId();
            }

            for (HashMap<String, String> hashMap : mygalleryparser.getParsedData()) {
                table_imagegallery imagegallery = new table_imagegallery();
                imagegallery.id = Integer.parseInt(hashMap.get("id"));

                if (Integer.parseInt(key) < 1) {
                    imagegallery.Delete();
                }
                imagegallery.imgageurl = hashMap.get("imageurlfromxml");

                imagegallery.Insert();
            }
            AsyncTaskResult<ArrayList<HashMap<String, String>>> myresult = new AsyncTaskResult<ArrayList<HashMap<String, String>>>(
                    mygalleryparser.getParsedData());
            return myresult;
        } catch (Exception e) {
            ArrayList<HashMap<String, String>> myresult = new ArrayList<HashMap<String, String>>();
            table_imagegallery forofflinefetch = new table_imagegallery();
            List<table_imagegallery> Off_imagegallery = new ArrayList<table_imagegallery>();

            switch (Integer.parseInt(Mysecondworker_key)) {
            case -1:
                if (Integer.parseInt(Mysecondworker_index) == 0)
                    Off_imagegallery = forofflinefetch.GetAllImages();
                break;
            }

            for (table_imagegallery imagegallery : Off_imagegallery) {
                HashMap<String, String> mapforimage = new HashMap<String, String>();
                mapforimage.put("id", String.valueOf(imagegallery.id));
                mapforimage.put("thumb_url1", imagegallery.imgageurl);
                myresult.add(mapforimage);
            }
            AsyncTaskResult<ArrayList<HashMap<String, String>>> myresultReturn = new AsyncTaskResult<ArrayList<HashMap<String, String>>>(
                    myresult , true);
            return myresultReturn;
        }
    }

    @Override
    protected void onPostExecute(
            AsyncTaskResult<ArrayList<HashMap<String, String>>> myresult) {

        Mysecondworker_progress.setVisibility(View.GONE);
        Mysecondworker_frameAnimation.stop();
        Mysecondworker_pagination.setVisibility(View.GONE);

        if (myresult.getError() != null) {

                //[[[errorr apear here]]]];
        } else {

            myAdapter mAd = (myAdapter) mystaggerdgridview.getAdapter();
            ArrayList<HashMap<String, String>> outputResult = new ArrayList<HashMap<String, String>>();
            if (mAd != null && mAd.getCount() > 0) {

                outputResult = mAd.getData();

            }
            outputResult.addAll(myresult.getResult());
            if (mAd == null) {
                myadapter = new myAdapter(targetCtx, outputResult);
                mystaggerdgridview.setAdapter(myadapter);
            } else {
                mAd.notifyDataSetChanged();
            }
            if (!myresult.checkforofline())
                targetCtx.setloadingisactive(false);
        }
    }
}

这是我的异步类:

public class AsyncTaskResult<T> {
    private T myresult = null;
    private Exception error = null;
    private boolean _checkforofline = false;

    public T getResult() {
        return myresult;
    }

    public Exception getError() {
        return error;
    }

    public AsyncTaskResult(T myresult) {
        super();
        this.myresult = myresult;
    }
    public AsyncTaskResult(T myresult,boolean isOffline) {
        super();
        this.myresult = myresult;
        _checkforofline = checkforofline;
    }

    public AsyncTaskResult() {
        super();
    }

    public AsyncTaskResult(Exception error) {
        super();
        this.error = error;
    }

    public boolean checkforofline() {
        return _checkforofline;
    }

和madapter类:

   public myAdapter(Activity act, ArrayList<HashMap<String, String>> listhast) {
        myactivity = act;
        mydata=listhast;
        myinflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myimageLoader=new ImageLoader(activity.getApplicationContext(),true);
    }

        private Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {

                }
            }
        };
        t.start();
        return t;
    }
    public int getCount() {
        return mydata.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public ArrayList<HashMap<String, String>> getData(){
        return mydata;
    }
     public View getView(final int position, View convertView, ViewGroup parent) {
        try {

                final myviewholder viewHolder;
                final HashMap<String, String> my_data_base_on_position = mydata.get(position);
                if (convertView == null) {
                    if(Integer.parseInt(my_data_base_on_position.get("id")) % 2 == 1){
                        convertView = mInflater.inflate(R.layout.layoutright, parent, false);
                        }else{
                            convertView = mInflater.inflate(R.layout.layoutleft, parent, false);
                        }
                    viewHolder = new myviewholder();
                    viewHolder.tv0 = (TextView) convertView.findViewById(R.id.textid);
                    viewHolder.imageurl = (ImageView) convertView.findViewById(R.id.image);
                    convertView.setTag(viewHolder);

                }else{
                    viewHolder = (myviewholder) convertView.getTag();
                }

                LayoutParams lp;
                if(data.size() > 0 ){

                    viewHolder.tv0.setText(my_data_base_on_position.get("id"));
                    viewHolder.tv0.setTag(my_data_base_on_position.get("id"));
                classtoloadimagefromurl.DisplayImage(my_data_base_on_position.get("imageurl"), viewHolder.imageurl);

                        lp = new LayoutParams(convertView.getLayoutParams());
                        lp.span = 1;

                        convertView.setLayoutParams(lp);

                return convertView;
                }else{
                    convertView = mInflater.inflate(R.layout.noimage, parent, false);
                    lp = new LayoutParams(convertView.getLayoutParams());
                    lp.span = 1;
                    convertView.setLayoutParams(lp);
                    return convertView;
                }

            }

如果我在我的madapter课程中使用listview而不是staggeredgrid,一切都很顺利,但是如果我选择在不均匀的ui中显示项目,那么当它尝试获取时会重复使用值!

这是错误:

01-09 11:51:52.656: E/Database(29557): Error inserting imagegallery
01-09 11:51:52.656: E/Database(29557): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
01-09 11:51:52.656: E/Database(29557):  at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
01-09 11:51:52.656: E/Database(29557):  at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
01-09 11:51:52.656: E/Database(29557):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1584)
01-09 11:51:52.656: E/Database(29557):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1428)
01-09 11:51:52.656: E/Database(29557):  at com.imagegallery.Tabel_for_images.Insert(Tabel_for_images.java:109)
01-09 11:51:52.656: E/Database(29557):  at com.imagegallery.Mysecondworker.doInBackground(Mysecondworker.java:141)
01-09 11:51:52.656: E/Database(29557):  at com.imagegallery.Mysecondworker.doInBackground(Mysecondworker.java:1)
01-09 11:51:52.656: E/Database(29557):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-09 11:51:52.656: E/Database(29557):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-09 11:51:52.656: E/Database(29557):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-09 11:51:52.656: E/Database(29557):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-09 11:51:52.656: E/Database(29557):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-09 11:51:52.656: E/Database(29557):  at java.lang.Thread.run(Thread.java:1019)

0 个答案:

没有答案