Draynaa
Draynaa

Reputation: 1

Android - nullPointerException on listview scroll

Having some trouble with my custom listview..

Each view has a on click listener which works fine, except for the views that can be seen after scrolling down, it throws a nullpointerexception when clicking one of these.

These views actually work if they can be seen before scrolling, so its a little confusing.

my code for the custom listview is shown below:

public class UserListView extends ArrayAdapter<User>{
int resource;
Context context;
String response;

public UserListView(Context context, int resource, List<User> users){
    super(context,resource,users);
    this.resource=resource;
}

public View getView(int position, View convertView, ViewGroup parent){
    LinearLayout userView;
    User user = getItem(position);  

    userView = new LinearLayout(getContext());
    String inflater = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater layoutInflate;
    layoutInflate = (LayoutInflater)getContext().getSystemService(inflater);
    layoutInflate.inflate(resource, userView, true);

    TextView name = (TextView) userView.findViewById(R.id.name);
    RatingBar userRating = (RatingBar) userView.findViewById(R.id.userRatingBar);
    name.setText(String.valueOf(user.getName()));

    float percentageNumber = ((float) user.getTotalQuestionsCorrect() / (float) user.getTotalQuestionsAnswered()) * 100;
    int percentage = (int)percentageNumber;
    setStars(userRating, percentage);
    userView.setId(user.getUserId());
    return userView;
}


private void setStars(RatingBar rb, int percent){
    RatingBar starRating = rb;
    int percentage = percent;

    if(percentage <= 5){starRating.setRating(0);}
    else if(percentage < 25){starRating.setRating(1);}
    else if(percentage >= 25 && percentage < 50){starRating.setRating(2);}
    else if(percentage >= 50 && percentage < 75){starRating.setRating(3);}
    else if(percentage >= 75 && percentage < 100){starRating.setRating(4);}
    else{starRating.setRating(5);}
}

}

Any help will be appreciated :) thanks

Upvotes: 0

Views: 1111

Answers (2)

Vicky
Vicky

Reputation: 5136

Override the getCount() method and return the length of the List.

public class UserListView extends ArrayAdapter<User>{
    int resource;
    Context context;
    String response;
    List<User> users;

    public UserListView(Context context, int resource, List<User> users){
        super(context,resource,users);
        this.resource=resource;
        this.users =users
       }

   @Override
    public int getCount() {
       return users.length;

       }

    }

Upvotes: 0

CaseyB
CaseyB

Reputation: 25058

There is a lot of weird stuff going on in your getView() method. I'm not sure that it's the problem but it's worth a try. The first thing is that convertView is a view that can be recycled to keep you from having to a new view for every item in your list. The other is that you create a LinearLayout to act as the parent view when the parent is passed into the getView() method. The third weird thing is that you're getting the LayoutInflator each time you need to inflate a View. You should really get it once in the constructor and then hold on to it. Try this:

public View getView(int position, View convertView, ViewGroup parent){
    User user = getItem(position);  
    if(convertView == null)
    {
        convertView = layoutInflate.inflate(resource, parent, false);
    }

    TextView name = (TextView) convertView.findViewById(R.id.name);
    RatingBar userRating = (RatingBar) convertView.findViewById(R.id.userRatingBar);
    name.setText(String.valueOf(user.getName()));

    float percentageNumber = ((float) user.getTotalQuestionsCorrect() / (float) user.getTotalQuestionsAnswered()) * 100;
    int percentage = (int)percentageNumber;
    setStars(userRating, percentage);
    convertView.setId(user.getUserId());
    return convertView;
}

Upvotes: 1

Related Questions