问题
I am trying to make a custom adapter using a view holder.
Log.i(CardHolderConstants.LOGIN_INFO, "cardsize>0");
myListAdapater=new MyListAdapater(this,cardList);
listview.setAdapter(myListAdapater);
Here i am calling the class mylist which extends baseadpater. CardList contains a list of card objects retrieved from the database.
static class ViewHolder{
public TextView cardId,cardNumber,cardName;
}
class MyListAdapater extends BaseAdapter{
List<Card> cardListForDisplay;
LayoutInflater inflater ; Context context;
public MyListAdapater(Context context,List<Card> cardListForDisplay) {
super();
Log.i(CardHolderConstants.LOGIN_INFO, "inside mylist");
this.cardListForDisplay = cardListForDisplay;
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return cardListForDisplay.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View view, ViewGroup parent) {
View rowView = view;
ViewHolder holder;
if(rowView==null){
holder=new ViewHolder();
inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.card_list, parent, false);
holder.cardId=(TextView)rowView.findViewById(R.id.displayCardID);
holder.cardName=(TextView)rowView.findViewById(R.id.displayCardName);
holder.cardNumber=(TextView)rowView.findViewById(R.id.displayCardNumber);
rowView.setTag(holder);
}
else{
holder=(ViewHolder) rowView.getTag();
}
Log.i(CardHolderConstants.CARD_HOLDER, "inside getView class");
for(Card card:cardListForDisplay){
if(card!=null){
long cardID= card.getCardId();
String cardName=card.getCardName();
long cardNumber=card.getCardNumber();
if(Utils.isNumberNullOrEmpty(cardID)==false && Utils.isNumberNullOrEmpty(cardNumber)==false
&& Utils.isStringNullOrEmpty(cardName)==false)
holder.cardId.setText(Long.toString(cardID));
holder.cardNumber.setText(Long.toString(cardNumber));
holder.cardName.setText(cardName);
}
}
return rowView;
}
}
}
when the class is initialized the get view method is not being called.
The log in the constructor gets printed.why is my getview
method not being called.
the list is being displayed . but the same item is being displayed. What is the correct way of iterating within the listview
回答1:
You have
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
Should be
@Override
public int getCount() {
// TODO Auto-generated method stub
return cardListForDisplay.size();
}
Also change to
public Object getItem(int arg0) {
return arg0; // can be cardListForDisplay.get(arg0)
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0; // can be cardListForDisplay.indexOf(getItem(arg0))
}
Edit:
Card card = (Card) cardListForDisplay.get(position);
long cardID= card.getCardId();
String cardName=card.getCardName();
long cardNumber=card.getCardNumber();
holder.cardId.setText(Long.toString(cardID));
holder.cardNumber.setText(Long.toString(cardNumber));
holder.cardName.setText(cardName);
回答2:
You have to change two things,
Change getItem method from,
@Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; }
to
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return cardListForDisplay.get(arg0);
}
- Your get View method should do the work for one view i.e. one row at a time. As this will be called for each row, so need of for loop there.
Change it to the below,
if(rowView==null){
holder=new ViewHolder();
inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.card_list, parent, false);
holder.cardId=(TextView)rowView.findViewById(R.id.displayCardID);
holder.cardName=(TextView)rowView.findViewById(R.id.displayCardName);
holder.cardNumber=(TextView)rowView.findViewById(R.id.displayCardNumber);
rowView.setTag(holder);
}
else{
holder=(ViewHolder) rowView.getTag();
}
Log.i(CardHolderConstants.CARD_HOLDER, "inside getView class");
Card card = getItem(position);
if(card!=null){
long cardID= card.getCardId();
String cardName=card.getCardName();
long cardNumber=card.getCardNumber();
if(Utils.isNumberNullOrEmpty(cardID)==false && Utils.isNumberNullOrEmpty(cardNumber)==false
&& Utils.isStringNullOrEmpty(cardName)==false)
holder.cardId.setText(Long.toString(cardID));
holder.cardNumber.setText(Long.toString(cardNumber));
holder.cardName.setText(cardName);
}
return rowView;
}
回答3:
Simply change here from
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
to
@Override
public int getCount() {
// TODO Auto-generated method stub
return cardListForDisplay.size();
}
also here
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
Finally Change this
class MyListAdapaterextends BaseAdapter{
to
class MyListAdapaterextends extends BaseAdapter{
回答4:
override 'getCount' properly .... check 'return' value of this method
回答5:
Your getCount()
method has the hard coded the return to 0 . So the ListAdapter
thinks there are no items in your list/array of list objects.
Change this:
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
to something like:
return sizeOfyourList;
来源:https://stackoverflow.com/questions/22317735/android-list-view-using-custom-adapter-and-view-holder