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;
}
之所以如此,是因为我不理解“骗局”方法?