2012-01-11 67 views
1

我已经显示了一个自定义列表视图,但是当我运行它时,列表视图项重复3次(可能是数组大小)。如何使列表视图显示非重复重复的项目?下面是我的代码。列表视图项重复在android

public class lvAdapter extends BaseAdapter implements OnClickListener { 
private Context context; 

String festi,weekd,datee; 
List<FestPOJO> listPhonebook = PublicHolidays.listOffests; 

public lvAdapter(Context context, List<FestPOJO> listPhonebook) { 
    this.context = context; 
    this.listPhonebook = listPhonebook; 
} 

public int getCount() { 
    return listPhonebook.size(); 
} 

public Object getItem(int position) { 
    return listPhonebook.get(position); 
} 

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


public View getView(int position, View convertView, ViewGroup viewGroup) { 


    FestPOJO inst = listPhonebook.get(position); 

    if (convertView == null) 

    { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.phone_row, null); 
    } 



    TextView tvContact = (TextView) convertView.findViewById(R.id.tvContact); 
    tvContact.setText(inst.getName()); 

    TextView tvPhone = (TextView) convertView.findViewById(R.id.tvMobile); 
    tvPhone.setText(inst.getDay()); 

    TextView tvMail = (TextView) convertView.findViewById(R.id.tvMail); 
    tvMail.setText(inst.getHalfdate()); 


    return convertView; 
} 

@Override 
public void onClick(View v) { 


} 

@Override 
public int getViewTypeCount() { 
    System.out.println("getviewtypecount"+getCount()); 
    return super.getViewTypeCount(); 
} 

    } 

这是我FestPOJO类:

public class FestPOJO { 
int weekend, opt, bank, year, id, date; 
String name, month, day,halfdate; 

public FestPOJO(String name, String day, String halfdate) { 

    this.name = name; 
    this.day = day; 
    this.halfdate = halfdate;  
} 

public String getHalfdate() { 
    return halfdate; 
} 

public void setHalfdate(String halfdate) { 
    this.halfdate = halfdate; 
} 

public int getWeekend() { 
    return weekend; 
} 

public void setWeekend(int weekend) { 
    this.weekend = weekend; 
} 

public int getOpt() { 
    return opt; 
} 

public void setOpt(int opt) { 
    this.opt = opt; 
} 

public int getBank() { 
    return bank; 
} 

public void setBank(int bank) { 
    this.bank = bank; 
} 

public int getYear() { 
    return year; 
} 

public void setYear(int year) { 
    this.year = year; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public int getDate() { 
    return date; 
} 

public void setDate(int date) { 
    this.date = date; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getMonth() { 
    return month; 
} 

public void setMonth(String month) { 
    this.month = month; 
} 

public String getDay() { 
    return day; 
} 

public void setDay(String day) { 
    this.day = day; 
} 

}

任何帮助是提前多appreciated.Thanks。

+0

阵列可能具有重复值之后错误的。所以必须确保数组中没有重复的条目。 – 2012-01-11 07:38:38

+0

@UsamaSarwar数组值不会重复。我通过在logcat中打印来检查它,并在sqlbrowser中查询它。 – 2012-01-11 09:08:09

回答

0

您正在通过重新使用现有视图来做正确的事情。您的底层列表是否可能包含重复项?在这种情况下,你必须筛选列表来抑制它们。

+0

数组值不会重复。我通过在logcat中打印来检查它,并在sqlbrowser中查询它。 – 2012-01-11 09:08:28

1

使用ViewHolder在适配器还是会有滚动列表视图

class MyAdapter extends BaseAdapter{ 
Context mContext; 
LinearLayout linearLayout = null; 
LayoutInflater inflater; 
TextView text; 

public MyAdapter(Context context) { 
    mContext = context; 
    inflater = LayoutInflater.from(mContext); 
} 

@Override 
public int getCount() { 
    return listString.size(); 
} 

@Override 
public int getItemViewType(int position) { 
    return positon; 
} 

@Override 
public Object getItem(int arg0) { 
    return listString.get(arg0); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    viewHolder1 holder1 = null;  

    if(convertView == null) 
    { 
     //no convertView create it the key is convertView.setTag 
     convertView = inflater.inflate(R.layout.listitem1, parent, false); 
     holder1 = new viewHolder(); 
     holder1.textView = (TextView)convertView.findViewById(R.id.textview1); 
     convertView.setTag(holder1); 
    } 
    else 
    { 
     //has convertViews the key is convertView.getTag(); 
     holder1 = (viewHolder1) convertView.getTag(); 
     holder1.textView.setText(Integer.toString(position)); 
    } 
    return convertView; 
} 

class viewHolder1{ 
    TextView textView; 
} 

}