为什么我在listview中获得重复的项目?

时间:2012-07-19 15:33:40

标签: android listview

我检查了数组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;
}
}

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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

 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...
 }

当View缓存(非空)

时执行

答案 2 :(得分: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)。我不能确切地说出你在做什么,所以我不确定它是如何需要的。