首页 > 移动开发 > android ListView 报错 The specified child already has a parent

android ListView 报错 The specified child already has a parent

今天遇到一个问题,在listviev中加入传进来的布局,上下滑动时,报错The specified child already has a parent。

先贴原始代码:

 

[java]
  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3.     convertView = layoutInflater.inflate(R.layout.check_laizi_item_ll, null);
  4.     RelativeLayout mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.clil_rl);
  5.     List<Poker> poker = mPokers.get(position);
  6.     for (int j = 0; j < poker.size(); j++) {
  7.         Poker card = poker.get(j);
  8.         if (card.isLaiZi()) {
  9.             card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResLaiZiID(), true));
  10.         } else {
  11.             card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResID(), true));
  12.         }
  13.         card.setId(position + 100);
  14.         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mst.adjustXIgnoreDensity(45), mst.adjustYIgnoreDensity(61));
  15.         params.topMargin = mst.adjustYIgnoreDensity(card_jiange);
  16.         params.leftMargin = mst.adjustXIgnoreDensity(card_jiange * j);
  17.         card.getInnerLayout().setBackgroundDrawable(ImageUtil.getResDrawable(R.drawable.poker_div, true));
  18.         card.getInnerLayout().setVisibility(View.GONE);
  19.         card.setClickable(false);
  20.         card.setLayoutParams(params);
  21.         mRelativeLayout.addView(card);
  22.     }
  23.     return convertView;
  24. }

 

再贴图片:

 

原因是listview中我的Poker控件被多个convertView引用。

之前以为是我的convertView框架被复用的原因,因此我不对convertView进行服用,每次都创建一个convertView,可是也会报这个错误。

我想问题应该在position上,具体神马情况不是我也不好解释,只是感觉position与convertView的变动没有及时同步吧

那我就把convertView用数组保存起来,然后根据position去取,这样是他们同步

 

再贴修改后代码:

 

[java]
  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3.     if(null==convertViews[position]){
  4.         convertView = layoutInflater.inflate(R.layout.check_laizi_item_ll, null);
  5.         RelativeLayout mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.clil_rl);
  6.         List<Poker> poker = mPokers.get(position);
  7.         for (int j = 0; j < poker.size(); j++) {
  8.             Poker card = poker.get(j);
  9.             if (card.isLaiZi()) {
  10.                 card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResLaiZiID(), true));
  11.             } else {
  12.                 card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResID(), true));
  13.             }
  14.             card.setId(position + 100);
  15.             RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mst.adjustXIgnoreDensity(45), mst.adjustYIgnoreDensity(61));
  16.             params.topMargin = mst.adjustYIgnoreDensity(card_jiange);
  17.             params.leftMargin = mst.adjustXIgnoreDensity(card_jiange * j);
  18.             card.getInnerLayout().setBackgroundDrawable(ImageUtil.getResDrawable(R.drawable.poker_div, true));
  19.             card.getInnerLayout().setVisibility(View.GONE);
  20.             card.setClickable(false);
  21.             card.setLayoutParams(params);
  22.             mRelativeLayout.addView(card);
  23.             convertViews[position]=convertView;
  24.         }
  25.     }else{
  26.         convertView=convertViews[position];
  27.     }
  28.     Log.i("positionss", "position:"+position+"       mPokers.size:"+mPokers.size()+"      convertViews[position]:"+convertViews[position]);
  29.     return convertView;
  30. }

本文固定链接: http://www.devba.com/index.php/archives/5187.html | 开发吧

报歉!评论已关闭.