Reputation: 3078
I have displayed a Custom List-View but when i run it, the list-view items repeated 3 times(might be the array size).How can i make the List-View display non-repeated items?Below is my code.
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();
}
}
This is my FestPOJO class:
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;
}
}
Any Help is much appreciated.Thanks in advance.
Upvotes: 1
Views: 1449
Reputation: 671
use a ViewHolder in the Adapter or there will be wrong after scroll the 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;
}
}
Upvotes: 1
Reputation: 12367
You are doing it right, by reusing existing views. Is it possible that your underlying lists contains duplicates? In this case you have to filter list to supress them.
Upvotes: 0