[英]Listview populate with ArrayList nullpointer
我正在尝试使用在这里找到的教程从arraylist进行listview。 这些行可以是团队名称或球员名称。 现在,当我向下滚动时,它可以正常工作,但是当我向上滚动时,它提供了一个空指针,通常是因为与名称相关联的行正试图设置团队名称的文本视图,反之亦然(我是从我的日志语句中确定的) 。 例如 将isPlayerName设置为true的视图在我设置viewholder.player_name的行上提供了一个空指针。 我认为这与没有引用正确对象的getTag()方法有关。 当我仅使用播放器行时,该教程运行良好(可能是因为所有Viewholder对象均被安装)。 这是我的listadapter:私有类CustomAdapter扩展了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;
}
}
这是我的viewHolder,我对两种类型的行都使用相同的值,但是只有某些字段会实例化,具体取决于是团队还是玩家行
private class ViewHolder {
ImageView team_icon;
TextView player_name;
RelativeLayout layout;
TextView team_name;
}
是否因为我不了解getTag()方法?
凯文
根据您的代码,您似乎正确地理解了getTag()
和setTag()
。 以防万一,让我为您分解一下:
“标签”是附加到另一个对象的对象(任何对象)。 它没有特定类型的配对,但通常用于将数据对象(例如viewHolder)与非数据对象(例如View)配对。 不幸的是,这意味着如果使用“标签”,则必须进行自己的类型检查。
“标签”最常见的问题是许多开发人员抓住了错误的View的“标签”,因此获得了错误的数据甚至是空数据。 您的代码似乎并非如此。 但是,您具有所有Log.d()
语句。 如果我们拥有该logcat的副本,我们当然可以为您提供更轻松的帮助。 眨眼眨眼
希望这可以帮助,
模糊逻辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.