简体   繁体   中英

Listview populate with ArrayList nullpointer

I'm trying to make a listview from an arraylist using the tutorial i found here . The rows can either be a team name or a player name. Right now it works fine when I scroll down, but gives a nullpointer when I scroll back up, usually because a row that is associated with a name is trying to set the team name textview or vice versa (i determined this from my log statements). Ex. a view with isPlayerName set to true gives a nullpointer on the line where I set viewholder.player_name. I think it has something to do with the getTag() method not referencing the right object. The tutorial worked fine when I used only player rows, (probably because all of viewholders objects were instaiated). Here is my listadapter: private class CustomAdapter extends BaseAdapter {

    private ArrayList<ArrayList<String>> player_array;
    private LayoutInflater inflater;

    public CustomAdapter(Context context, ArrayList<ArrayList<String>> array){
        player_array = array;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return player_array.size();
    }

    @Override
    public Object getItem(int position) {
        return player_array.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewholder= new ViewHolder();
        boolean isPlayerName = (player_array.get(position).size() > 1 ? true : false);
        if (convertView == null){
        if (isPlayerName){
            convertView = inflater.inflate(R.layout.player_list_item, null);
            viewholder.player_name = (TextView)convertView.findViewById(R.id.player_name);
            viewholder.layout = (RelativeLayout)convertView.findViewById(R.id.player_list_item_layout);
        }
        else {
            convertView = inflater.inflate(R.layout.team_item, null);
            viewholder.team_icon = (ImageView)convertView.findViewById(R.id.team_logo);
            viewholder.team_name = (TextView)convertView.findViewById(R.id.team_name);
        }
        convertView.setTag(viewholder);
        }
        else {
            viewholder = (ViewHolder)convertView.getTag();
        }
        if (isPlayerName){
            try {
                viewholder.player_name.setText((player_array.get(position)).get(0));
                viewholder.layout.setOnClickListener(new CustomOnClickListener(player_array.get(position).get(1)));
            }
            catch (NullPointerException e){
                Log.d("NULL", "is viewholder null? " + (viewholder == null));
                Log.d("NULL", "is player name? " + isPlayerName);
                Log.d("NULL", "is getTag null? " + (convertView.getTag() == null));
                Log.d("NULL", "is team name null " + (viewholder.team_name == null));
                Log.d("NULL", "is team icon null " + (viewholder.team_icon == null));
                Log.d("NULL", "is player name null " + (viewholder.player_name == null));
                Log.d("NULL", "is link null? " + (player_array.get(position).get(1) == null));
                Log.d("NULL", "is layout null? " + (viewholder.layout == null));
            }

        }
        else {
            String team_name = player_array.get(position).get(0);
            try {
            viewholder.team_name.setText(Character.toUpperCase(team_name.charAt(0)) + team_name.substring(1));
            }
            catch (NullPointerException e){
                Log.d("NULL", "is player name? " + isPlayerName);
                Log.d("NULL", "is getTag null? " + (convertView.getTag() == null));
                Log.d("NULL", "is team name null " + (viewholder.team_name == null));
                Log.d("NULL", "is team icon null " + (viewholder.team_icon == null));
                Log.d("NULL", "is player name null " + (viewholder.player_name == null));
            }
            if (team_name.equals("hawks")){
                viewholder.team_icon.setBackgroundResource(R.drawable.hawks);
            }
            else if (team_name.equals("lions")){
                viewholder.team_icon.setBackgroundResource(R.drawable.lions);
            }
            else if (team_name.equals("sparks")){
                viewholder.team_icon.setBackgroundResource(R.drawable.sparks);
            }
            else if (team_name.equals("bulls")){
                viewholder.team_icon.setBackgroundResource(R.drawable.bulls);
            }
            else if (team_name.equals("renegades")){
                viewholder.team_icon.setBackgroundResource(R.drawable.renegades);
            }
            else if (team_name.equals("poppiezz")){
                viewholder.team_icon.setBackgroundResource(R.drawable.poppiezz);
            }
            else if (team_name.equals("mambas")){
                viewholder.team_icon.setBackgroundResource(R.drawable.mambas);
            }
            else if (team_name.equals("78sixers")){
                viewholder.team_icon.setBackgroundResource(R.drawable.sixers);
            }
            else if (team_name.equals("brooklynites")){
                viewholder.team_icon.setBackgroundResource(R.drawable.brooklynites);
            }
            else if (team_name.equals("blazers")){
                viewholder.team_icon.setBackgroundResource(R.drawable.blazers);
            }
            else if (team_name.equals("warriors")){
                viewholder.team_icon.setBackgroundResource(R.drawable.warriors);
            }
            else {
                viewholder.team_icon.setBackgroundResource(R.drawable.basketball);
            }
        }
        return convertView;
    }

}

And here is my viewHolder, I use the same one for both types of rows, but only certain fields get instantiated depending on if it is a team or a player row

    private class ViewHolder {

    ImageView team_icon;
    TextView player_name;
    RelativeLayout layout;
    TextView team_name;
}

Is it because I am not understanding the getTag() method?

Kevin,

According to your code, you seem to be understanding getTag() and setTag() correctly. Just in case, let me break it down for you:

A "tag" is an Object (any Object) that is attached to another. It has no type specific pairing, but often it is used to pair data-objects (like your viewHolder) to non data-objects (like a View). Unfortunately, this means that if using a "tag" you must do your own type checking.

The most common issue with "tags" is that many developers grab the "tag" of the wrong View and, hence, get the wrong or even null data. This does not seem to be the case with your code. However, you have all of the Log.d() statements. We would certainly be able to help you more easily if we had a copy of that logcat. wink wink nudge nudge

Hope this helps,

FuzzicalLogic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM