我已经显示了一个自定义列表视图,但是当我运行它时,列表视图项重复了3次(可能是数组大小)。如何让列表视图显示非重复项?下面是我的代码。
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;
}
}
非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
在适配器中使用ViewHolder,或者滚动listView
后会出错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;
}
}
答案 1 :(得分:0)
您正在通过重复使用现有视图来做到这一点。您的基础列表是否可能包含重复项?在这种情况下,你必须过滤列表来压制它们。