簡體   English   中英

列表視圖項在android中重復

[英]List view items repeats in android

我已經顯示了一個自定義列表視圖,但是當我運行它時,列表視圖項重復了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;
}

}

非常感謝您的幫助。

在適配器中使用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;  
} 

}

通過重用現有視圖,您做對了。 您的基礎列表是否可能包含重復項? 在這種情況下,您必須過濾列表以抑制它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM