Android RecyclerView Adapter notifyItemChanged() 获取 IllegalStateException 已经在池中

Posted

技术标签:

【中文标题】Android RecyclerView Adapter notifyItemChanged() 获取 IllegalStateException 已经在池中【英文标题】:Android RecyclerView Adapter notifyItemChanged() getting IllegalStateException Already in the pool 【发布时间】:2018-07-10 12:44:25 【问题描述】:

我正在使用 Recyclerview 并通过 notifyItemChanged() 对其进行更新 使用线性布局管理器的recyclerView 当从套接字更新数据时,我通过适配器 notifyItemChanged() 更新回收器 套接字每秒发送许多数据,这意味着我每秒调用适配器多次 它可以工作,但是当更新更多次时会出现此错误

异常 java.lang.IllegalStateException:已经在池中! android.support.v7.widget.AdapterHelper.recycleUpdateOp (AdapterHelper.java:742) android.support.v7.widget.AdapterHelper.dispatchAndUpdateViewHolders (AdapterHelper.java:294) android.support.v7.widget.AdapterHelper.applyUpdate (AdapterHelper.java:220) android.support.v7.widget.AdapterHelper.preProcess (AdapterHelper.java:104) android.support.v7.widget.RecyclerView.consumePendingUpdateOperations (RecyclerView.java:1696) android.support.v7.widget.RecyclerView$1.run (RecyclerView.java:346) android.view.Choreographer$CallbackRecord.run (Choreographer.java:959) android.view.Choreographer.doCallbacks (Choreographer.java:734) android.view.Choreographer.doFrame (Choreographer.java:667) android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:945) android.os.Handler.handleCallback (Handler.java:751) android.os.Handler.dispatchMessage (Handler.java:95) android.os.Looper.loop (Looper.java:154) android.app.ActivityThread.main (ActivityThread.java:6776) java.lang.reflect.Method.invoke (Method.java) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1518) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)

我的适配器

public class Companies_Adapter extends 
RecyclerView.Adapter<Companies_Adapter.ViewHolder> 
private View view;
boolean isFirstTime = false;
private ArrayList<Company> companies;
private Context context;
private LayoutInflater mInflater;
ArrayList<Company> arraylist;

public Companies_Adapter(ArrayList<Company> chat_items, Context context) 
    this.companies = chat_items;
    this.context = context;
    mInflater = LayoutInflater.from(context);
    arraylist = new ArrayList<>();
    arraylist.addAll(chat_items);



@Override
public long getItemId(int i) 
    return i;


@Override
public int getItemCount() 
    return companies.size();


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 
    public TextView LastTradePrice ,DailyHighPrice  ,DailyLowPrice  ,DiffLastPricePercent
            ,AskPrice ,AskVol ,BidPrice ,BidVol ,NameA,liquidity,liquidity_type,company_id ;

    public ViewHolder(View itemView) 
        super(itemView);
        LastTradePrice = (TextView)itemView.findViewById(R.id.LastTradePrice);
        DiffLastPricePercent = (TextView)itemView.findViewById(R.id.DiffLastPricePercent);
        AskPrice= (TextView)itemView.findViewById(R.id.AskPrice);
        AskVol = (TextView)itemView.findViewById(R.id.AskVol);
        BidPrice = (TextView)itemView.findViewById(R.id.BidPrice);
        BidVol = (TextView)itemView.findViewById(R.id.BidVol);
        NameA = (TextView)itemView.findViewById(R.id.NameA);
        liquidity = (TextView)itemView.findViewById(R.id.liquidity);
        liquidity_type = (TextView)itemView.findViewById(R.id.liquidity_type);
        DailyHighPrice = (TextView)itemView.findViewById(R.id.highpric);
        DailyLowPrice = (TextView)itemView.findViewById(R.id.lowprice);
        company_id = (TextView)itemView.findViewById(R.id.company_id);
    



    @Override
    public void onClick(View v) 
        //  this.itemClickListener.onItemClick(v, getLayoutPosition());

    


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    View view = mInflater.inflate(R.layout.company_item, parent, false);
    return new ViewHolder(view);


@Override
public void onBindViewHolder(final ViewHolder holder, final int position) 
    Typeface lightFace = Typeface.createFromAsset(context.getAssets(), "fonts/Arial-BoldMT.otf");

    holder.company_id.setTextSize(TypedValue.TYPE_STRING,6);
    holder.AskPrice.setTextSize(TypedValue.TYPE_STRING,6);
    holder.DailyHighPrice.setTextSize(TypedValue.TYPE_STRING,6);
    holder.DailyLowPrice.setTextSize(TypedValue.TYPE_STRING,6);
    holder.AskVol.setTextSize(TypedValue.TYPE_STRING,6);
    holder.BidPrice.setTextSize(TypedValue.TYPE_STRING,6);
    holder.BidVol.setTextSize(TypedValue.TYPE_STRING,6);
    holder.DiffLastPricePercent.setTextSize(TypedValue.TYPE_STRING,6);
    holder.NameA.setTextSize(TypedValue.TYPE_STRING,7);
    holder.liquidity.setTextSize(TypedValue.TYPE_STRING,7);
    holder.liquidity_type.setTextSize(TypedValue.TYPE_STRING,6);
    holder.LastTradePrice.setTextSize(TypedValue.TYPE_STRING,7);
    holder.NameA.setTypeface(lightFace);
    holder.liquidity.setTypeface(lightFace);
    holder.LastTradePrice.setTypeface(lightFace);

    NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);

    DecimalFormat precision = (DecimalFormat)nf;
    precision.applyPattern("0.00");

        if (companies.get(position).isChanged()) 
          if(companies.get(position).isFirstTime()) 
              holder.liquidity_type.setText(companies.get(position).getLiquidity_status());
              if (companies.get(position).getLiquidity_status().equals("0.00"))
                  holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.green));
              else 
                  if (companies.get(position).getLiquidity_status().equals("in")) 
                      holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.green));
                   else 
                      holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.red));
                  
              
              holder.DailyHighPrice.setText(precision.format(Double.parseDouble(companies.get(position).getDailyHighPrice())));
              holder.DailyLowPrice.setText(precision.format(Double.parseDouble(companies.get(position).getDailyLowPrice())));
              holder.DailyLowPrice.setTextColor(context.getResources().getColor(R.color.red));
              holder.DailyHighPrice.setTextColor(context.getResources().getColor(R.color.green));
              holder.company_id.setText(companies.get(position).getStockCode());

               if (companies.get(position).getLastTradePrice() != null) 
                  holder.LastTradePrice.setText(precision.format(Double.parseDouble(companies.get(position).getLastTradePrice())));
              
              if (companies.get(position).getDiffLastPricePercent() != null) 
                  holder.DiffLastPricePercent.setText
                          (precision.format(Double.valueOf(companies.get(position)
                                  .getDiffLastPricePercent())));
                  if (!companies.get(position).getDiffLastPricePercent().contains("-")) 
                      holder.DiffLastPricePercent.setTextColor(context.getResources().getColor(R.color.green));
                      holder.LastTradePrice.setTextColor(context.getResources().getColor(R.color.green));
                      holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.green));
                      new CountDownTimer(400, 50) 
                          @Override
                          public void onTick(long arg0) 
                              // TODO Auto-generated method stub
                          

                          @Override
                          public void onFinish() 
                              holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.black));
                              companies.get(position).setChanged(false);
                          
                      .start();


                   else 
                      holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.red));
                      new CountDownTimer(400, 50) 

                          @Override
                          public void onTick(long arg0) 
                              // TODO Auto-generated method stub
                          

                          @Override
                          public void onFinish() 
                              holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.black));
                              companies.get(position).setChanged(false);
                          

                      .start();
                      holder.DiffLastPricePercent.setTextColor(context.getResources().getColor(R.color.red));
                      holder.LastTradePrice.setTextColor(context.getResources().getColor(R.color.orange));
                  
              

              if (companies.get(position).getAskPrice() != null)
                  holder.BidPrice.setText(precision.format(Double.parseDouble(companies.get(position).getAskPrice())));
              if (companies.get(position).getAskVol() != null && !companies.get(position).getAskVol().equals(""))
                  holder.BidVol.setText(NumberFormat.getNumberInstance(Locale.US).format(Double.valueOf(
                          companies.get(position).getAskVol())));
              if (companies.get(position).getBidPrice() != null)
                  holder.AskPrice.setText(companies.get(position).getBidPrice());
              if (companies.get(position).getBidVol() != null && !companies.get(position).getBidVol().equals(""))
                  holder.AskVol.setText(NumberFormat.getNumberInstance(Locale.US).format(Double.valueOf(
                          companies.get(position).getBidVol())));
              double thero = companies.get(position).getTermo();

              holder.liquidity.setText(precision.format(thero));
              if (thero >= 0.4 && thero < 0.8) 
                  holder.liquidity.setBackgroundResource(R.color.beige);
                  holder.liquidity.setTextColor(context.getResources().getColor(R.color.green_dark));
              

              if (thero >= 0.8 && thero < 1.8) 
                  holder.liquidity.setBackgroundResource(R.color.yellow);
                  holder.liquidity.setTextColor(Color.BLUE);
              
              if (thero >= 1.8 && thero < 2.8) 
                  holder.liquidity.setBackgroundColor(Color.BLUE);
                  holder.liquidity.setTextColor(Color.WHITE);
              
              if (thero >= 2.8) 
                  holder.liquidity.setBackgroundResource(R.color.black);
                  holder.liquidity.setTextColor(context.getResources().getColor(R.color.gold));
              
              if (thero < 0.4 && thero >= 0) 
                  holder.liquidity.setTextColor(Color.GREEN);
                  holder.liquidity.setBackgroundResource(R.color.black);

              
              if (thero < 0) 
                  holder.liquidity.setTextColor(Color.RED);
                  holder.liquidity.setBackgroundResource(R.color.black);

              
              holder.liquidity_type.setText(companies.get(position).getLiquidity_status());

              if (companies.get(position).getNameA() != null)
                  holder.NameA.setText(companies.get(position).getNameA());

              holder.itemView.setOnClickListener(new View.OnClickListener() 
                  @Override
                  public void onClick(View view) 
                      Intent intent = new Intent("aaaa");
                      intent.putExtra("name", companies.get(position).getNameA());
                      intent.putExtra("cprice", companies.get(position).getLastTradePrice());
                      intent.putExtra("cpercentage", companies.get(position).getDiffLastPricePercent());
                      intent.putExtra("openning_price", companies.get(position).getOpenning_price());
                      intent.putExtra("lower_price", companies.get(position).getDailyLowPrice());
                      intent.putExtra("deals", companies.get(position).getDeals_count());
                      intent.putExtra("value", companies.get(position).getValue());
                      intent.putExtra("higher", companies.get(position).getDailyHighPrice());
                      intent.putExtra("lastclose", companies.get(position).getLast_close());
                      intent.putExtra("size", companies.get(position).getSize());
                      intent.putExtra("point", companies.get(position).getCreation());
                      intent.putExtra("ss", companies.get(position).getSs());
                      intent.putExtra("strs", companies.get(position).getSTrs());
                      intent.putExtra("inout", holder.liquidity_type.getText().toString());
                      intent.putExtra("sb", companies.get(position).getSb());
                      intent.putExtra("ts", companies.get(position).getTs());
                      intent.putExtra("net_liquidity", companies.get(position).getSyola_Net());
                      intent.putExtra("stock_code", companies.get(position).getStockCode());
                      intent.putExtra("thero", companies.get(position).getTermo());
                      intent.putExtra("pos", position);
                      BroadcastHelper.sendInform(context, "open_company", intent);
                      BroadcastHelper.sendInform(context, "closedialog");

                  
              );

           else 
              holder.liquidity_type.setText(companies.get(position).getLiquidity_status());

              if (companies.get(position).getLiquidity_status().equals("0.00"))
                  holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.green));
              else 
                  if (companies.get(position).getLiquidity_status().equals("in")) 
                      holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.green));
                   else 
                      holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.red));
                  
              
              holder.DailyHighPrice.setText(precision.format(Double.parseDouble(companies.get(position).getDailyHighPrice())));
              holder.DailyLowPrice.setText(precision.format(Double.parseDouble(companies.get(position).getDailyLowPrice())));
              holder.DailyLowPrice.setTextColor(context.getResources().getColor(R.color.red));
              holder.DailyHighPrice.setTextColor(context.getResources().getColor(R.color.green));
              holder.company_id.setText(companies.get(position).getStockCode());
              companies.get(position).setFirstTime(true);

              if (companies.get(position).getLastTradePrice() != null) 
                  holder.LastTradePrice.setText(precision.format(Double.parseDouble(companies.get(position).getLastTradePrice())));
              
              if (companies.get(position).getDiffLastPricePercent() != null) 

                  holder.DiffLastPricePercent.setText
                          (precision.format(Double.valueOf(companies.get(position)
                                  .getDiffLastPricePercent())));
                  if (!companies.get(position).getDiffLastPricePercent().contains("-")) 
                      holder.DiffLastPricePercent.setTextColor(context.getResources().getColor(R.color.green));
                      holder.LastTradePrice.setTextColor(context.getResources().getColor(R.color.green));
                   else 
                      holder.DiffLastPricePercent.setTextColor(context.getResources().getColor(R.color.red));
                      holder.LastTradePrice.setTextColor(context.getResources().getColor(R.color.orange));
                  
              
              if (companies.get(position).getAskPrice() != null)
                  holder.BidPrice.setText(companies.get(position).getAskPrice());
              if (companies.get(position).getAskVol() != null && !companies.get(position).getAskVol().equals(""))
                  holder.BidVol.setText(NumberFormat.getNumberInstance(Locale.US).format(Double.valueOf(
                          companies.get(position).getAskVol())));
              if (companies.get(position).getBidPrice() != null)
                  holder.AskPrice.setText(precision.format(Double.parseDouble(companies.get(position).getBidPrice())));
              if (companies.get(position).getBidVol() != null && !companies.get(position).getBidVol().equals(""))
                  holder.AskVol.setText(NumberFormat.getNumberInstance(Locale.US).format(Double.valueOf(
                          companies.get(position).getBidVol())));

            double thero = companies.get(position).getTermo();
              holder.liquidity.setText(precision.format(thero));
              if (thero >= 0.4 && thero < 0.8) 
                  holder.liquidity.setBackgroundResource(R.color.beige);
                  holder.liquidity.setTextColor(context.getResources().getColor(R.color.green_dark));
              

              if (thero >= 0.8 && thero < 1.8) 
                  holder.liquidity.setBackgroundResource(R.color.yellow);
                  holder.liquidity.setTextColor(Color.BLUE);
              
              if (thero >= 1.8 && thero < 2.8) 
                  holder.liquidity.setBackgroundColor(Color.BLUE);
                  holder.liquidity.setTextColor(Color.WHITE);
              
              if (thero >= 2.8) 
                  holder.liquidity.setBackgroundResource(R.color.black);
                  holder.liquidity.setTextColor(context.getResources().getColor(R.color.gold));
              
              if (thero < 0.4 && thero >= 0) 
                  holder.liquidity.setTextColor(Color.GREEN);
                  holder.liquidity.setBackgroundResource(R.color.black);

              
              if (thero < 0) 
                  holder.liquidity.setTextColor(Color.RED);
                  holder.liquidity.setBackgroundResource(R.color.black);

              

              if (companies.get(position).getNameA() != null)
                  holder.NameA.setText(companies.get(position).getNameA());

              holder.itemView.setOnClickListener(new View.OnClickListener() 
                  @Override
                  public void onClick(View view) 
                      Intent intent = new Intent("aaaa");
                      intent.putExtra("name", companies.get(position).getNameA());
                      intent.putExtra("cprice", companies.get(position).getLastTradePrice());
                      intent.putExtra("cpercentage", companies.get(position).getDiffLastPricePercent());
                      intent.putExtra("openning_price", companies.get(position).getOpenning_price());
                      intent.putExtra("lower_price", companies.get(position).getDailyLowPrice());
                      intent.putExtra("deals", companies.get(position).getDeals_count());
                      intent.putExtra("value", companies.get(position).getValue());
                      intent.putExtra("higher", companies.get(position).getDailyHighPrice());
                      intent.putExtra("lastclose", companies.get(position).getLast_close());
                      intent.putExtra("size", companies.get(position).getSize());
                      intent.putExtra("point", companies.get(position).getCreation());
                      intent.putExtra("ss", companies.get(position).getSs());
                      intent.putExtra("strs", companies.get(position).getSTrs());
                      intent.putExtra("inout", holder.liquidity_type.getText().toString());
                      intent.putExtra("sb", companies.get(position).getSb());
                      intent.putExtra("ts", companies.get(position).getTs());
                      intent.putExtra("net_liquidity", companies.get(position).getSyola_Net());
                      intent.putExtra("stock_code", companies.get(position).getStockCode());
                      intent.putExtra("thero", companies.get(position).getTermo());
                      intent.putExtra("pos", position);
                      BroadcastHelper.sendInform(context, "open_company", intent);
                      BroadcastHelper.sendInform(context, "closedialog");
                  
              );
          
         else 
            holder.liquidity_type.setText(companies.get(position).getLiquidity_status());
            holder.DailyHighPrice.setText(precision.format(Double.parseDouble(companies.get(position).getDailyHighPrice())));
            holder.DailyLowPrice.setText(precision.format(Double.parseDouble(companies.get(position).getDailyLowPrice())));
            holder.DailyLowPrice.setTextColor(context.getResources().getColor(R.color.red));
            holder.DailyHighPrice.setTextColor(context.getResources().getColor(R.color.green));
            holder.company_id.setText(companies.get(position).getStockCode());
            if (companies.get(position).getLastTradePrice() != null)
                holder.LastTradePrice.setText(precision.format(Double.parseDouble(companies.get(position).getLastTradePrice())));
            if (companies.get(position).getDiffLastPricePercent() != null) 
                holder.DiffLastPricePercent.setText
                        (precision.format(Double.valueOf(companies.get(position)
                                .getDiffLastPricePercent())));

                if (!companies.get(position).getDiffLastPricePercent().contains("-")) 
                    holder.DiffLastPricePercent.setTextColor(context.getResources().getColor(R.color.green));
                    holder.LastTradePrice.setTextColor(context.getResources().getColor(R.color.green));
                 else 
                    holder.DiffLastPricePercent.setTextColor(context.getResources().getColor(R.color.red));
                    holder.LastTradePrice.setTextColor(context.getResources().getColor(R.color.orange));
                
            

            if (companies.get(position).getAskPrice() != null)
                holder.BidPrice.setText(precision.format(Double.parseDouble(companies.get(position).getAskPrice())));
            if (companies.get(position).getAskVol() != null && !companies.get(position).getAskVol().equals(""))
                holder.BidVol.setText(NumberFormat.getNumberInstance(Locale.US).format(Double.valueOf(
                        companies.get(position).getAskVol())));
            if (companies.get(position).getBidPrice() != null)
                holder.AskPrice.setText(precision.format(Double.parseDouble(companies.get(position).getBidPrice())));
            if (companies.get(position).getBidVol() != null && !companies.get(position).getBidVol().equals(""))
                holder.AskVol.setText(NumberFormat.getNumberInstance(Locale.US).format(Double.valueOf(
                        companies.get(position).getBidVol())));
            double thero = companies.get(position).getTermo();
            holder.liquidity.setText(precision.format(companies.get(position).getTermo()));
            if (thero >= 0.4 && thero < 0.8) 
                holder.liquidity.setBackgroundResource(R.color.beige);
                holder.liquidity.setTextColor(context.getResources().getColor(R.color.green_dark));
            
            if (thero >= 0.8 && thero < 1.8) 
                holder.liquidity.setBackgroundResource(R.color.yellow);
                holder.liquidity.setTextColor(Color.BLUE);
            
            if (thero >= 1.8 && thero < 2.8) 
                holder.liquidity.setBackgroundColor(Color.BLUE);
                holder.liquidity.setTextColor(Color.WHITE);
            
            if (thero >= 2.8) 
                holder.liquidity.setBackgroundResource(R.color.black);
                holder.liquidity.setTextColor(context.getResources().getColor(R.color.gold));
            
            if (thero < 0.4 && thero >= 0) 
                holder.liquidity.setTextColor(Color.GREEN);
                holder.liquidity.setBackgroundResource(R.color.black);

            
            if (thero < 0) 
                holder.liquidity.setTextColor(Color.RED);
                holder.liquidity.setBackgroundResource(R.color.black);
            
            holder.liquidity_type.setText(companies.get(position).getLiquidity_status());
            if(companies.get(position).getLiquidity_status().equals("in"))
                holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.green));
            else 
                holder.liquidity_type.setTextColor(context.getResources().getColor(R.color.red));
            
            if (companies.get(position).getNameA() != null)
                holder.NameA.setText(companies.get(position).getNameA());

            holder.itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    Intent intent = new Intent("aaaa");
                    intent.putExtra("name", companies.get(position).getNameA());
                    intent.putExtra("cprice", companies.get(position).getLastTradePrice());
                    intent.putExtra("cpercentage", companies.get(position).getDiffLastPricePercent());
                    intent.putExtra("openning_price", companies.get(position).getOpenning_price());
                    intent.putExtra("lower_price", companies.get(position).getDailyLowPrice());
                    intent.putExtra("deals", companies.get(position).getDeals_count());
                    intent.putExtra("value", companies.get(position).getValue());
                    intent.putExtra("higher", companies.get(position).getDailyHighPrice());
                    intent.putExtra("lastclose", companies.get(position).getLast_close());
                    intent.putExtra("size", companies.get(position).getSize());
                    intent.putExtra("point",companies.get(position).getCreation());
                    intent.putExtra("ss",companies.get(position).getSs());
                    intent.putExtra("strs",companies.get(position).getSTrs());
                    intent.putExtra("inout",holder.liquidity_type.getText().toString());
                    intent.putExtra("sb",companies.get(position).getSb());
                    intent.putExtra("ts",companies.get(position).getTs());
                    intent.putExtra("thero",companies.get(position).getTermo());
                     intent.putExtra("net_liquidity",companies.get(position).getSyola_Net());
                    intent.putExtra("stock_code",companies.get(position).getStockCode());
                    intent.putExtra("pos",position);
                    BroadcastHelper.sendInform(context, "open_company", intent);
                    BroadcastHelper.sendInform(context, "closedialog");
                
            );
        
 

@Override
public int getItemViewType(int position) 
    return position;


【问题讨论】:

【参考方案1】:

经过多次搜索并尝试多种解决方案 最后我找到了解决方案 我将 ReyclerView 更改为 listView 并通过此代码更新我的列表

if (itemPos >= company_recycler.getFirstVisiblePosition() && itemPos-1<company_recycler.getLastVisiblePosition())
                                    final View cell = company_recycler.getChildAt(itemPos - company_recycler.getFirstVisiblePosition() );
                                    runOnUiThread(new Runnable() 
                                        @Override
                                        public void run() 
                                            adapter.getView(itemPos-1, cell, company_recycler);
                                        
                                    );

这对我来说很好

【讨论】:

不切换到列表视图是否可以解决此崩溃问题?

以上是关于Android RecyclerView Adapter notifyItemChanged() 获取 IllegalStateException 已经在池中的主要内容,如果未能解决你的问题,请参考以下文章

Android中RecyclerView嵌套RecyclerView或嵌套ListView

Android RecyclerView嵌套RecyclerView

Android之RecyclerView介绍

Android:RecyclerView里面的RecyclerView,滚动到底部

Android教程2020 - RecyclerView获取滑动距离

android recyclerview怎么设置每次只能滑动一页