与上添加getview动态绘制()导致显示错误的数据更新适配器项 [英] Update adapter items with add dynamic drawable on getview() lead to show wrong data

查看:110
本文介绍了与上添加getview动态绘制()导致显示错误的数据更新适配器项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义适配器,列出我的项目。在每一个项目我检查数据库,并绘制一些圆圈的颜色。

正如你在code见我检查 convertView == NULL 定义新viewHolder和借鉴我的项目。但是当我的列表视图滚动非常快速绘制的每个数据(而不是标题和文本)显示是非!结果
我怎么能没有数据显示错误管理动态视图创建?!

更新

这是我的尝试:


  1. 我用UI线程来更新我的名单,但结果是相同的,数据绘制出问题。

  2. 在第二个我试着让没有必要适配器检查数据库加载所有数据和我的对象。但问题仍然存在...

  3. 最后我创建的HashMap<键,&的LinearLayout GT; 并以其项目ID缓存每个绘制布局。所以,如果之前我只是从我的HashMap的加载其观点和每一个动态布局将创造只是一次它的绘制。但它仍然显示了快速滚动错误的数据!我真的不知道下一步该怎么做!

    @覆盖
        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){

     最后ViewHolder viewHolder;
        最后MenuStructureCase项目=的getItem(位置);        如果(convertView == NULL){            viewHolder =新ViewHolder();
                convertView = this.mInflater.inflate(R.layout.fragment_menu_item,NULL);
                viewHolder.menu_title =(TextView中)convertView.findViewById(R.id.menu_title);
                viewHolder.tag_list_in_menu_linear_layout =(的LinearLayout)convertView.findViewById(R.id.tag_list_in_menu_linear_layout);
                viewHolder.menu_delete =(的ImageButton)convertView.findViewById(R.id.image_button_delete);
                importMenuTags(viewHolder,的getItem(位置),viewHolder.tag_list_in_menu_linear_layout);
                convertView.setTag(viewHolder);        }其他{
                viewHolder =(ViewHolder)convertView.getTag();        }        viewHolder.menu_title.setText(item.getTitle());
        }
        返回convertView;
    }


这是importMenuTags():

 私人无效importMenuTags(ViewHolder viewHolder,MenuStructureCase项目的LinearLayout布局){
        清单<串GT;标记= db.getMenuTags(item.getTitle()); //这个$ P串$ psent列表包含我的标签        对于(字符串标签:标签){            。可绘制可绘制=的getContext()getResources()getDrawable(R.drawable.color_shape);
            drawable.setColorFilter(Color.parseColor(each_tag_color),PorterDuff.Mode.SRC_ATOP);            RelativeLayout的RL =新的RelativeLayout(的getContext());
            LinearLayout.LayoutParams lparams =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            lparams.setMargins(15,15,15,15);
            lparams.width = 50;
            lparams.height = 50;
            如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.JELLY_BEAN){
                rl.setBackground(绘制);
            }其他{
                rl.setBackgroundDrawable(绘制);
            }
            rl.setLayoutParams(lparams);
            layout.addView(RL);
        }    }


解决方案

您有适配器初始化之前选择DB数据。这样

 的getItem(位置)

已经将返回准备就绪的项目对象。

您不应该设置在

的值查看

 如果(convertView == NULL){
    ...
}

这code是只为 viewHolder 初始化。创建一个新的,如果 convertView 或把它读作标记。
viewHolder 初始化后,你所要做的值设置,其实你设置的称号。
但是为了提高性能,你不应该选择 getView 的每一步,从分贝值。你必须拥有一切prepared(已选定)。

I have a custom adapter that list my items. in each Item I check database and draw some circles with colors.

As you see in code I check if convertView==null defines new viewHolder and draw my items. but when I scroll listview very fast every drawn data ( not title and texts) show wrongs!
How I can manage dynamic View creation without showing wrong data?!

UPDATE

This is my attempts:

  1. I used ui-thread to update my list but the result is same and data drawing go wrong.
  2. in second I try to load all data with my object so that there is no need to check db in adapter. but it problem is still remains...
  3. finally I create the HashMap<key,LinearLayout> and cache every drawn layout with id of its item. So if it's drawn before I just load its view from my HashMap and every dynamic layout will create just once. But it still shows wrong data on fast scrolling! Really I don't know what to do next!

    @Override public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;
        final MenuStructureCase item = getItem(position);
    
            if (convertView == null) {
    
                viewHolder = new ViewHolder();
                convertView = this.mInflater.inflate(R.layout.fragment_menu_item, null);
                viewHolder.menu_title = (TextView) convertView.findViewById(R.id.menu_title);
                viewHolder.tag_list_in_menu_linear_layout = (LinearLayout) convertView.findViewById(R.id.tag_list_in_menu_linear_layout);
                viewHolder.menu_delete = (ImageButton) convertView.findViewById(R.id.image_button_delete);
                importMenuTags(viewHolder, getItem(position), viewHolder.tag_list_in_menu_linear_layout);
                convertView.setTag(viewHolder);
    
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
    
            }
    
            viewHolder.menu_title.setText(item.getTitle());
    
    
        }
        return convertView;
    }
    

and this is importMenuTags():

   private void importMenuTags(ViewHolder viewHolder, MenuStructureCase item, LinearLayout layout) {


        List<String> tags = db.getMenuTags(item.getTitle());  //this present list of string that contain my tags

        for (String tag : tags) {

            Drawable drawable = getContext().getResources().getDrawable(R.drawable.color_shape);
            drawable.setColorFilter(Color.parseColor(each_tag_color), PorterDuff.Mode.SRC_ATOP);

            RelativeLayout rl = new RelativeLayout(getContext());
            LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lparams.setMargins(15, 15, 15, 15);
            lparams.width = 50;
            lparams.height = 50;


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                rl.setBackground(drawable);
            } else {
                rl.setBackgroundDrawable(drawable);
            }
            rl.setLayoutParams(lparams);
            layout.addView(rl);
        }

    }

解决方案

You have to select data from db before adapter initialization. So that

getItem(position) 

will return already a "ready" item-object.

You shouldn't set the values to Views inside

if (convertView == null) {
    ...
}

This code is only for a viewHolder initialization. You create a new one, if convertView is null or read it as tag. Setting of values you have to do after viewHolder initialization, actually where you set the title. But in order to increase a performance, you shouldn't select the values from db on each step of getView. You have to have everything prepared (already selected).

这篇关于与上添加getview动态绘制()导致显示错误的数据更新适配器项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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