开发者

EfficientAdapter and text format

I am using EfficientAdapter that extends ListActivity. For some reason, when I use code like:

  public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.text.setPadding((int) (Vars.screenWid-300), 30, 0, 30);
            if (position==1){
                holder.text.setPadding(20, 0, 20, 0);
                holder.text.setBackgroundColor(Color.DKGRAY);
            }else{
          开发者_JAVA百科      holder.text.setPadding(20, 20, 20, 20);
                 holder.text.setBackgroundColor(Color.TRANSPARENT);
            }
        }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text.setText(String.valueOf(Vars.DATAMIN[position]));
        return convertView;
    }

I get the list, but instead of just item 1 with Color.DKGRAY, I get it RANDOMALLY, on other items. I scroll up and down and see the background change from one item to another. ANy ideas?


The views are recycled(meaning you are setting the color of views that are going to show up in positions other than 1) so for that to work properly, you need to move the code where you change the padding and color based on position to outside of the convertView == null block.

if (convertView == null) {
    convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
    holder = new ViewHolder();
    holder.text = (TextView) convertView.findViewById(R.id.text);
    holder.text.setPadding((int) (Vars.screenWid-300), 30, 0, 30);
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}
if (position==1){
    holder.text.setPadding(20, 0, 20, 0);
    holder.text.setBackgroundColor(Color.DKGRAY);
}else{
    holder.text.setPadding(20, 20, 20, 20);
    holder.text.setBackgroundColor(Color.TRANSPARENT);
}
holder.text.setText(String.valueOf(Vars.DATAMIN[position]));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜