当前位置: 首页 > 知识库问答 >
问题:

Android RecyclerView适配器notifyItemChanged()正在获取池中已经存在的IllegalStateException

马高谊
2023-03-14

Iam使用RecolyerView并通过notifyItemChanged()更新数据时使用linearlayoutmanager使用RecolyerView从套接字更新数据时使用适配器更新RecolyerNotifyItemChanged()套接字每秒发送许多数据,这意味着我每秒多次调用适配器它正在工作,但当更新次数更多时会出现此错误

异常java.lang.IllegalStateException:已在池中!Android.support.v7.widget.adapterhelper.recycleupdateop(adapterhelper.java:742)Android.support.v7.widget.adapterhelper.dispatchandupdateviewowler(adapterhelper.java:294)Android.support.v7.widget.adapterhelper.applyupdate(adapterhelper.java:220)Android.support.v7.widget.adapterhelper.preprocess(adapterhelper.java:104)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个答案

方苗宣
2023-03-14

经过多次搜索,尝试了多次解决方案,终于找到了解决方案,我将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);
                                        }
                                    });

这对我很好

 类似资料:
  • 在旧代码中,当我使用与自定义适配器时,我可以使用此代码获取项目。 现在我正在实现。如何从<代码>回收视图中获取项目。适配器?

  • 我有一个使用RecyclerView的列表,我想要的是,当我点击列表项时,它会打开一个片段,我一直在搜索,我只在一个活动中找到了它,但我只在片段中工作,如果我使用活动,它会随着我的操作栏消失,它都在片段中设置。 这是我的适配器类: 我需要将信用卡id发送到片段,调用控制器并从数据库中获取信息,我尝试了使用intent,但只有在使用startActivity(intent)调用活动时才有效。 这里是

  • 我试图使用picasso库将url加载到imageView,但无法获得来正确使用picasso库。

  • AdapterClass: 提前感谢你的帮助。

  • 我有一个连接到CustomAdapter的ListView。在Adapter类中,我有不同视图的侦听器。当某些事情发生变化时,它会更新ListItem的值 现在我不知道如何将这些信息返回到包含ListView的片段中。我试过了 但它不会对适配器内的任何更改作出反应。仅限于片段本身的更改(如单击添加新项的按钮)。 我还想知道如何获取适配器类中最新ArrayList的引用,以便将其返回到片段。 我希望

  • 知道吗?redisConnectionFactory bean中的主机名是集群的配置endpoint。