2

我检查了数组 menuItems 看起来不错,但是下面的代码有问题。当我开始时它似乎还可以,但是当我开始滚动时它会搞砸并将项目放在列表顶部并重复项目......这很奇怪

public class ScoresAdapter extends BaseAdapter {

private MainActivity activity;
private ScoreItem[] menuItems;
private static LayoutInflater inflater=null;
ImageDownloader downloader;

public ScoresAdapter(MainActivity a, ScoreItem[] scoreItems) {
    activity = a;
    menuItems = scoreItems;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    downloader = new ImageDownloader();
}

public int getCount() {
    int teller = 0;

    for(int i = 0 ; menuItems.length > i ; i++){
        if(menuItems[i] != null){
            teller++;
        }
    }

    return teller;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {        
    View vi=convertView;
    if(convertView==null){    
        if(menuItems[position].getId() == 888){
            //fav header
            vi = inflater.inflate(R.layout.scoresheaderfavoriet, null);

        }else if(menuItems[position].getId() == 999){
            //overige header
            vi = inflater.inflate(R.layout.scoresheaderoverig, null);

        }else{



            if(menuItems[position].getIsFav()){
                vi = inflater.inflate(R.layout.scoresfavitem, null);
            }else{
                vi = inflater.inflate(R.layout.scoresitem, null);
            }

            TextView text2=(TextView)vi.findViewById(R.id.position);
            text2.setText(""+ menuItems[position].getPosition());

            TextView text=(TextView)vi.findViewById(R.id.name);
            text.setText(menuItems[position].getFirstName() + " " + menuItems[position].getLastName().toUpperCase());

            TextView text5=(TextView)vi.findViewById(R.id.country);
            text5.setText(""+ menuItems[position].getCountry());

            TextView text3=(TextView)vi.findViewById(R.id.score);
            text3.setText(""+ menuItems[position].getScore());

            TextView text8=(TextView)vi.findViewById(R.id.hole);
            text8.setText(""+ menuItems[position].getHole());

            vi.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), SpelerView.class);
                    intent.putExtra("id", menuItems[position].getId());
                    v.getContext().startActivity(intent);

                    activity.sluitAlleMenus();
                }
            });
        }


    }



    return vi;
}
}
4

3 回答 3

2

我有同样的问题

    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    CalendarsData c = (CalendarsData) this.getItem(position);

    if (convertView == null) {
        holder=new ViewHolder();
        convertView = l_Inflater.inflate(R.layout.preference_list_element, null);

        holder.text=(CheckBox)convertView.findViewById(R.id.checkTextView);         
        //holder.tv =(TextView)convertView.findViewById(R.id.textview);

        holder.text.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                CalendarsData ssi = (CalendarsData) cb.getTag();
                ssi.setChecked(cb.isChecked());
            }
        });             

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }   

holder.text.setText("c.anyvalue");
return convertView;
}               

就像上面的人说你需要其他:

} else {
        holder = (ViewHolder) convertView.getTag();
于 2013-03-07T14:39:44.997 回答
0

我曾经遇到过同样的问题。我认为这是因为适配器的“缓存”......你应该实现这样的 else :

 if(convertView==null){
     holder = new NodeViewHolder();
     //Your stuff
     convertView.setTag(holder);
 }else{
     holder = (NodeViewHolder) convertView.getTag();
 }

 public static class NodeViewHolder {
    TextView text;
    TextView text2;
    TextView text3;
    //So on...
 }

缓存视图时执行(非空)

于 2012-07-19T15:43:12.657 回答
0

当您的 convertView 不正确为空时,您没有处理这种情况。像这样更改您的代码:

public View getView(final int position, View convertView, ViewGroup parent) {        
    View vi=convertView;
    if(convertView==null){    
        if(menuItems[position].getId() == 888){
            //fav header
            vi = inflater.inflate(R.layout.scoresheaderfavoriet, null);

        }else if(menuItems[position].getId() == 999){
            //overige header
            vi = inflater.inflate(R.layout.scoresheaderoverig, null);

        }else{



            if(menuItems[position].getIsFav()){
                vi = inflater.inflate(R.layout.scoresfavitem, null);
            }else{
                vi = inflater.inflate(R.layout.scoresitem, null);
            }


        }


    }
            TextView text2=(TextView)vi.findViewById(R.id.position);
            text2.setText(""+ menuItems[position].getPosition());

            TextView text=(TextView)vi.findViewById(R.id.name);
            text.setText(menuItems[position].getFirstName() + " " + menuItems[position].getLastName().toUpperCase());

            TextView text5=(TextView)vi.findViewById(R.id.country);
            text5.setText(""+ menuItems[position].getCountry());

            TextView text3=(TextView)vi.findViewById(R.id.score);
            text3.setText(""+ menuItems[position].getScore());

            TextView text8=(TextView)vi.findViewById(R.id.hole);
            text8.setText(""+ menuItems[position].getHole());

            vi.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), SpelerView.class);
                    intent.putExtra("id", menuItems[position].getId());
                    v.getContext().startActivity(intent);

                    activity.sluitAlleMenus();
                }
            });





    return vi;
}

这将使您最容易到达那里。您可能还必须将一些您的收藏夹和/或页眉页脚逻辑移出if(convertView ==null)。不过,我不能确切地说出你在做什么,所以我不确定它需要如何。

于 2012-07-19T15:47:00.083 回答