无尽适配器始终运行下载后台数据 [英] Endless adapter always running to download data in background

查看:253
本文介绍了无尽适配器始终运行下载后台数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定制EndlessAdapter的code在我的应用程序,但通过我做的,因为这无尽的适配器一个错误随时下载数据的背景,而应用户滚动起伏列表之后下载相同的,我无法找到同样的错误在我们的项目。由于这个错误我的应用程序有时会返回OutOfMemoryException异常这是不能接受的。

请给我建议就同一任何解决方案。

code:
*的 MyEndlessAdapter:的*

 类DemoAdapterCat扩展EndlessAdapter {
              私人RotateAnimation旋转= NULL;
              ArrayList的<串GT; tempListNamesCat =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListImagesCat =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListYearCat1 =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListYearCat2 =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListmpgCat1 =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListmpgCat2 =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListpriceCat1 =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListpriceCat2 =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListRatingCat1 =新的ArrayList<串GT;();
              ArrayList的<串GT; tempListRatingCat2 =新的ArrayList<串GT;();              DemoAdapterCat(){
               超(新CategoryListLazyAdapter(ResearchList.this,countriesSubCat,imagesSubCat,YearSubCat1,YearSubCat2,mpgSubCat1,mpgSubCat2,priceSubCat1,priceSubCat2,ratingSubCat1,ratingSubCat2));
               实用UTIL =新的实用程序();
                util.setListViewHeightBasedOnChildren(lvCategory);
               旋转=新RotateAnimation(0F,360F,Animation.RELATIVE_TO_SELF,
                 0.5F,Animation.RELATIVE_TO_SELF,
                 0.5F);
               rotate.setDuration(600);
               rotate.setRepeatMode(Animation.RESTART);
               rotate.setRepeatCount(Animation.INFINITE);
              }
            / *
              @覆盖
            公众诠释getCount将()
              {
                  //返回brandList.getDisplayNames()的大小()。
                  返回countriesSubCat.size();
              } * /
              @覆盖
              受保护的视图getPendingView(ViewGroup中父){
               行= getLayoutInflater()膨胀(R.layout.categorylist,NULL);               孩子= row.findViewById(R.id.tv_CategoryItem_Name);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_MPG1);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_MPG2);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_Price1);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_Price2);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_Rating1);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_Rating2);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_Year1);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.tv_CategoryItem_Year2);
               child.setVisibility(View.GONE);
               / *子= row.findViewById(R.id.img_CategoryItem);
               child.setVisibility(View.GONE);
               孩子= row.findViewById(R.id.img_CategoryItem_Arrow);
               child.setVisibility(View.GONE); * /               / *子= row.findViewById(R.id.linear_main_MPG);
               child.setVisibility(View.GONE); * /
               孩子= row.findViewById(R.id.throbber);
               child.setVisibility(View.VISIBLE);
               child.startAnimation(旋转);               返回(行);
              }              @覆盖
              保护布尔cacheInBackground(){                    SystemClock.sleep(10000);
               tempListNamesCat.clear();
               tempListImagesCat.clear();
               tempListmpgCat1.clear();
               tempListmpgCat2.clear();
               tempListpriceCat1.clear();
               tempListpriceCat2.clear();
               tempListYearCat1.clear();
               tempListYearCat2.clear();
               tempListRatingCat1.clear();
               tempListRatingCat2.clear();
               INT lastOffset = getLastOffset();
               如果(lastOffset< LIST_SIZE){
                INT上限= lastOffset + BATCH_SIZE;
                的for(int i =(lastOffset + 1);(I< =极限和放大器;&安培; I< LIST_SIZE);我++){
                 tempListNamesCat.add(coll.getDisplayNames()获得(一));
                 tempListImagesCat.add(coll.getImages()获得(一));
                 tempListmpgCat1.add(coll.getMpg()获得(一));
                 tempListmpgCat2.add(coll.getMpg()获得第(i + 1));
                 tempListpriceCat1.add(coll.getPrice()获得(一));
                 tempListpriceCat2.add(coll.getPrice()获得第(i + 1));
                 tempListRatingCat1.add(coll.getRating()获得(一));
                 tempListRatingCat2.add(coll.getRating()获得第(i + 1));
                 tempListYearCat1.add(coll.getYears()获得(一));
                 tempListYearCat2.add(coll.getYears()获得第(i + 1));
                }
                setLastOffset(限制);                如果(限< LIST_SIZE){
                 //返回true;
                    返回(getWrappedAdapter()的getCount()&所述; coll.getDisplayNames()大小());
                }其他{
                 返回false;
                }
               }其他{
                返回false;
               }
              }
              @覆盖
              保护无效appendCachedData(){               @燮pressWarnings(未登记)
               //活动活动=这一点;
               // ArrayAdapter<串GT; arrAdapterNew =(ArrayAdapter<串GT;)getWrappedAdapter();
             CategoryListLazyAdapter arrAdapterNewCategory =(CategoryListLazyAdapter)getWrappedAdapter();
               // INT listLen = tempList.size();
              // INT listLen = tempListNames.size();
              countriesSubCat.addAll(tempListNamesCat);
              imagesSubCat.addAll(tempListImagesCat);
              mpgSubCat1.addAll(tempListmpgCat1);
              mpgSubCat2.addAll(tempListmpgCat2);
              priceSubCat1.addAll(tempListpriceCat1);
              priceSubCat2.addAll(tempListpriceCat2);
              ratingSubCat1.addAll(tempListRatingCat1);
              ratingSubCat2.addAll(tempListRatingCat2);
              YearSubCat1.addAll(tempListYearCat1);
              YearSubCat2.addAll(tempListYearCat2);
              arrAdapterNewCategory.notifyDataSetChanged();
              实用UTIL =新的实用程序();
              util.setListViewHeightBasedOnChildren(lvCategory);
              / *的for(int i = 0; I< listLen;我++){
              // arrAdapterNew.add(tempList.get(I));
               } * /
              }
             }

设置Demoadapter到列表:

 公共无效setValues​​InCategoryChildSortByAZ(字符串URL,最终字符串过滤器,从最终的字符串,最后字符串)
    {
        如果(isOnline())
        {
            最后ProgressDialog对话框= ProgressDialog.show(
                    ResearchList.this,研究名单,
                    请稍候...,真正的);            最后的处理程序处理程序=新的处理程序(){                公共无效的handleMessage(消息MSG){                    //的System.out.println(保存后的ID:+ id.get(0)的ToString());                    // catagory.addAll(keyword_vector1);
                    linear_Category_Child.setVisibility(View.GONE);
                    linear_Category_Child_Child.setVisibility(View.VISIBLE);
                    //tv_Child_Header.setText(\"Volvo);
                    / *适配器=新CategoryListLazyAdapter(
                            ResearchList.this);
                    lvCategory.setAdapter(适配器); * /
                    demoAdapterCat.notifyDataSetChanged();
                    实用UTIL =新的实用程序();
                    util.setListViewHeightBasedOnChildren(lvCategory);
                    dialog.dismiss();
                }
            };            最终螺纹checkUpdate =新主题(){                公共无效的run(){
                    尝试{
                        字符串sortEn code = URLEn coder.en code(阿尔法);
                        字符串filterEn code = URLEn coder.en code(过滤器);
                        字符串clientEn code = URLEn coder.en code(10030812);
                        字符串fromEn code = URLEn coder.en code(从);
                        字符串TOEN code = URLEn coder.en code(至);
                        字符串catUrl = \"/v1/vehicles/get-make-models.json?sort=\"+sortEn$c$c+\"&filter=\"+filterEn$c$c+\"&client-id=\"+clientEn$c$c+\"&from=\"+fromEn$c$c;
                        genSig =新GetSignature(catUrl,讴歌);
                        尝试{
                            签名= genSig.getUrlFromString();
                        }赶上(InvalidKeyException将E1){
                            // TODO自动生成catch块
                            e1.printStackTrace();
                        }赶上(抛出:NoSuchAlgorithmException E1){
                            // TODO自动生成catch块
                            e1.printStackTrace();
                        }
                        // jsonString =
                        // getJsonSring(\"http://api.highgearmedia.com/v1/vehicles/get-models.json?make=acura&client-id=10030812&signature=LWQbdAlJVxlXZ1VO2mfqAA==\");
                        //字符串signatureEn code = URLEn coder.en code(签字);
                        字符串urlEn code = URLEn coder.en code(catUrl +&放大器;签名=+签字);
                        jsonString = getJsonSring(http://apibeta.highgearmedia.com+ catUrl +&放大器;签名=+签字);
                        的System.out.println(JSON类别:===>中+ jsonString);
                    }赶上(ClientProtocolException E){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }赶上(的URISyntaxException E){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }赶上(IOException异常五){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }
                    JsonParse JSON =新JsonParse(jsonString);
                    json.parseCat();
                    handler.sendEmptyMessage(0);                }            };
            checkUpdate.start();
        }其他
        {
            AlertDialog.Builder建设者=新生成器(ResearchList.this);
            builder.setTitle(注意!);
            builder.setMessage(网络连接不可用。);
            builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){
                @覆盖
                公共无效的onClick(DialogInterface对话,诠释它){
                    // TODO自动生成方法存根                }
            });
            builder.create()显示()。
        }    }

先谢谢了。


解决方案

  

我无法找到同样的错误在我们的项目


该EndlessAdapter示例应用程序是在这里: https://github.com/commonsguy / CWAC无头/株/主/演示

如果你看一下示例应用程序,你会发现,它并没有任何派生线程。

如果您阅读 EndlessAdapter 文件,您将看到:


  

您EndlessAdapter子类还需要实现cacheInBackground()。这个方法从后台线程中调用,它需要下载,最终将被添加到您在构造函数中使用的ListAdapter更多的数据。虽然演示应用程序只需睡10秒,一个真正的应用程序可能会在更多的数据调用Web服务或其他负载。


  
  

由于这种方法被称为后台线程,你不需要到餐桌自己的线程。然而,在同一时间,不要试图直接更新UI。


因此​​,改变你的项目无法派生自己的线程,而是做你的工作在 cacheInBackground()来检索和补充下一批数据。

如果这个方法不适合你的应用程序需要运行的方式,这很好,但你可能有困难,使用 EndlessAdapter

I have customized the code of EndlessAdapter in my app but one mistake done by me as this Endless Adapter always download data in the background instead it should download the same after user scroll downs the list and I unable to find the same mistake in our project. Due to this mistake my app sometimes returns OutOfMemoryException which is not acceptable.

Please suggest me any solution regarding the same.

Code: *MyEndlessAdapter:*

 class DemoAdapterCat extends EndlessAdapter {
              private RotateAnimation rotate=null;
              ArrayList<String> tempListNamesCat = new ArrayList<String>();
              ArrayList<String> tempListImagesCat = new ArrayList<String>();
              ArrayList<String> tempListYearCat1 = new ArrayList<String>();
              ArrayList<String> tempListYearCat2 = new ArrayList<String>();
              ArrayList<String> tempListmpgCat1 = new ArrayList<String>();
              ArrayList<String> tempListmpgCat2 = new ArrayList<String>();
              ArrayList<String> tempListpriceCat1 = new ArrayList<String>();
              ArrayList<String> tempListpriceCat2 = new ArrayList<String>();
              ArrayList<String> tempListRatingCat1 = new ArrayList<String>();
              ArrayList<String> tempListRatingCat2 = new ArrayList<String>();

              DemoAdapterCat() {
               super(new CategoryListLazyAdapter(ResearchList.this, countriesSubCat, imagesSubCat, YearSubCat1,YearSubCat2, mpgSubCat1,mpgSubCat2, priceSubCat1,priceSubCat2, ratingSubCat1, ratingSubCat2));
               Utility util = new Utility();
                util.setListViewHeightBasedOnChildren(lvCategory);
               rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                 0.5f, Animation.RELATIVE_TO_SELF,
                 0.5f);
               rotate.setDuration(600);
               rotate.setRepeatMode(Animation.RESTART);
               rotate.setRepeatCount(Animation.INFINITE);
              }
            /* 
              @Override
            public int getCount()
              {
                  //return brandList.getDisplayNames().size();
                  return countriesSubCat.size();
              }*/
              @Override
              protected View getPendingView(ViewGroup parent) {
               row=getLayoutInflater().inflate(R.layout.categorylist, null);

               child=row.findViewById(R.id.tv_CategoryItem_Name);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_MPG1);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_MPG2);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_Price1);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_Price2);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_Rating1);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_Rating2);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_Year1);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.tv_CategoryItem_Year2);
               child.setVisibility(View.GONE);
               /*child = row.findViewById(R.id.img_CategoryItem);
               child.setVisibility(View.GONE);
               child = row.findViewById(R.id.img_CategoryItem_Arrow);
               child.setVisibility(View.GONE);*/

               /*child = row.findViewById(R.id.linear_main_MPG);
               child.setVisibility(View.GONE);*/
               child=row.findViewById(R.id.throbber);
               child.setVisibility(View.VISIBLE);
               child.startAnimation(rotate);

               return(row);
              }

              @Override
              protected boolean cacheInBackground() {

                    SystemClock.sleep(10000); 
               tempListNamesCat.clear();
               tempListImagesCat.clear();
               tempListmpgCat1.clear();
               tempListmpgCat2.clear();
               tempListpriceCat1.clear();
               tempListpriceCat2.clear();
               tempListYearCat1.clear();
               tempListYearCat2.clear();
               tempListRatingCat1.clear();
               tempListRatingCat2.clear();
               int lastOffset = getLastOffset();
               if(lastOffset < LIST_SIZE){
                int limit = lastOffset + BATCH_SIZE;
                for(int i=(lastOffset+1); (i<=limit && i<LIST_SIZE); i++){
                 tempListNamesCat.add(coll.getDisplayNames().get(i));
                 tempListImagesCat.add(coll.getImages().get(i));
                 tempListmpgCat1.add(coll.getMpg().get(i));
                 tempListmpgCat2.add(coll.getMpg().get(i+1));
                 tempListpriceCat1.add(coll.getPrice().get(i));
                 tempListpriceCat2.add(coll.getPrice().get(i+1));
                 tempListRatingCat1.add(coll.getRating().get(i));
                 tempListRatingCat2.add(coll.getRating().get(i+1));
                 tempListYearCat1.add(coll.getYears().get(i));
                 tempListYearCat2.add(coll.getYears().get(i+1));
                }    
                setLastOffset(limit);

                if(limit<LIST_SIZE){
                 //return true;
                    return(getWrappedAdapter().getCount()<coll.getDisplayNames().size());
                } else {
                 return false;
                }
               } else  {
                return false;
               }
              }


              @Override
              protected void appendCachedData() {

               @SuppressWarnings("unchecked")
               //Activity activity = this;
               //ArrayAdapter<String> arrAdapterNew = (ArrayAdapter<String>)getWrappedAdapter();
             CategoryListLazyAdapter arrAdapterNewCategory = (CategoryListLazyAdapter)getWrappedAdapter();
               //int listLen = tempList.size();
              // int listLen = tempListNames.size();
              countriesSubCat.addAll(tempListNamesCat);
              imagesSubCat.addAll(tempListImagesCat);
              mpgSubCat1.addAll(tempListmpgCat1);
              mpgSubCat2.addAll(tempListmpgCat2);
              priceSubCat1.addAll(tempListpriceCat1);
              priceSubCat2.addAll(tempListpriceCat2);
              ratingSubCat1.addAll(tempListRatingCat1);
              ratingSubCat2.addAll(tempListRatingCat2);
              YearSubCat1.addAll(tempListYearCat1);
              YearSubCat2.addAll(tempListYearCat2);
              arrAdapterNewCategory.notifyDataSetChanged();
              Utility util = new Utility();
              util.setListViewHeightBasedOnChildren(lvCategory);
              /* for(int i=0; i<listLen; i++){
              //  arrAdapterNew.add(tempList.get(i));
               }*/
              }
             }

Set Demoadapter to the list:

public void setValuesInCategoryChildSortByAZ(String url,final String filter, final String from, final String to)
    {
        if(isOnline())
        {
            final ProgressDialog dialog = ProgressDialog.show(
                    ResearchList.this, "Research List       ",
                    "Please wait...   ", true);

            final Handler handler = new Handler() {

                public void handleMessage(Message msg) {

                    // System.out.println("The id after Save:"+id.get(0).toString());

                    // catagory.addAll(keyword_vector1);
                    linear_Category_Child.setVisibility(View.GONE);
                    linear_Category_Child_Child.setVisibility(View.VISIBLE);
                    //tv_Child_Header.setText("Volvo");
                    /*adapter = new CategoryListLazyAdapter(
                            ResearchList.this);
                    lvCategory.setAdapter(adapter);*/
                    demoAdapterCat.notifyDataSetChanged();
                    Utility util = new Utility();
                    util.setListViewHeightBasedOnChildren(lvCategory);
                    dialog.dismiss();
                }
            };

            final Thread checkUpdate = new Thread() {

                public void run() {
                    try {
                        String sortEncode = URLEncoder.encode("alpha");
                        String filterEncode = URLEncoder.encode(filter);
                        String clientEncode = URLEncoder.encode("10030812");
                        String fromEncode = URLEncoder.encode(from);
                        String toEncode = URLEncoder.encode(to);
                        String catUrl = "/v1/vehicles/get-make-models.json?sort="+sortEncode+"&filter="+filterEncode+"&client-id="+clientEncode+"&from="+fromEncode;
                        genSig = new GetSignature(catUrl, "acura");
                        try {
                            signature = genSig.getUrlFromString();
                        } catch (InvalidKeyException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (NoSuchAlgorithmException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        // jsonString =
                        // getJsonSring("http://api.highgearmedia.com/v1/vehicles/get-models.json?make=acura&client-id=10030812&signature=LWQbdAlJVxlXZ1VO2mfqAA==");
                        //String signatureEncode = URLEncoder.encode(signature);
                        String urlEncode = URLEncoder.encode(catUrl+"&signature="+signature);
                        jsonString = getJsonSring("http://apibeta.highgearmedia.com"+catUrl+"&signature="+signature);
                        System.out.println("The json category:===>"+jsonString);
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    JsonParse json = new JsonParse(jsonString);
                    json.parseCat();
                    handler.sendEmptyMessage(0);

                }

            };
            checkUpdate.start();
        }else
        {
            AlertDialog.Builder builder = new Builder(ResearchList.this);
            builder.setTitle("Attention!");
            builder.setMessage("Network Connection unavailable.");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {


                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });


            builder.create().show();
        }

    }

Thanks in advance.

解决方案

I unable to find the same mistake in our project

The EndlessAdapter sample app is here: https://github.com/commonsguy/cwac-endless/tree/master/demo.

If you look at that sample app, you will notice that it does not fork any threads.

If you read the EndlessAdapter documentation, you will see:

Your EndlessAdapter subclass also needs to implement cacheInBackground(). This method will be called from a background thread, and it needs to download more data that will eventually be added to the ListAdapter you used in the constructor. While the demo application simply sleeps for 10 seconds, a real application might make a Web service call or otherwise load in more data.

Since this method is called on a background thread, you do not need to fork your own thread. However, at the same time, do not try to update the UI directly.

So, change your project to not fork your own thread, but instead do your work in cacheInBackground() to retrieve and add the next batch of data.

If that approach does not fit the way your app needs to run, that is fine, but you may have difficulty in using EndlessAdapter.

这篇关于无尽适配器始终运行下载后台数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆