Java 使用ArrayList空指针填充Listview
我正在尝试使用我找到的教程从arraylist生成listview。行可以是球队名称或球员名称。现在,当我向下滚动时,它可以正常工作,但当我向上滚动时,它会给出一个空指针,通常是因为与名称关联的行正在尝试设置团队名称textview,反之亦然(我是根据日志语句确定的)。例如,isPlayerName设置为true的视图在我设置viewholder.player\u name的行上给出一个空指针。我认为这与getTag()方法没有引用正确的对象有关。当我只使用播放器行时,教程运行良好(可能是因为所有的ViewHolder对象都已安装)。这是我的listadapter: 私有类CustomAdapter扩展了BaseAdapter{Java 使用ArrayList空指针填充Listview,java,android,Java,Android,我正在尝试使用我找到的教程从arraylist生成listview。行可以是球队名称或球员名称。现在,当我向下滚动时,它可以正常工作,但当我向上滚动时,它会给出一个空指针,通常是因为与名称关联的行正在尝试设置团队名称textview,反之亦然(我是根据日志语句确定的)。例如,isPlayerName设置为true的视图在我设置viewholder.player\u name的行上给出一个空指针。我认为这与getTag()方法没有引用正确的对象有关。当我只使用播放器行时,教程运行良好(可能是因为所
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;
}
}
是因为我不理解getTag()方法吗?Kevin
根据您的代码,您似乎正确地理解了getTag()
和setTag()
。以防万一,让我为您详细介绍一下:
“标记”是附加到另一个对象的对象(任何对象)。它没有特定类型的配对,但通常用于将数据对象(如视图持有者)与非数据对象(如视图)配对。不幸的是,这意味着如果使用“标记”,则必须自己进行类型检查
“标记”最常见的问题是,许多开发人员抓住了错误视图的“标记”,因此获得了错误甚至空的数据。您的代码似乎不是这样。但是,您拥有所有的Log.d()
语句。如果我们有一个logcat.wink-wink-nudge-nudge的副本,我们当然可以更轻松地帮助您
希望这有帮助
FuzzicalLogicah这也许可以解释。因此,基本上配对与hashmap的配对不同?它可以这样使用,但实际上并非如此。多个对象可能具有相同的标记。给定对象甚至可能具有多个标记(类似地分配给hashmap).本质上,如果你保持一个标签对一个视图的比率…是的,它可以是一个hashmap。但是它没有hashmap类型保护。我考虑了一下,决定使用一个通用的viewholder来避免空错误,因为我以前的方法可能不值得修复。不过感谢你的解释,我会投票赞成清除my混淆。@Kevin Qiu我很抱歉把这个挖出来!我也有同样的问题,虽然我理解这个问题,但我不确定我是否理解你的解决方案。你能具体说明一下“universal viewholder”是什么意思吗?谢谢!我基本上只是使用一个viewholder作为模板,然后根据列表中的项目隐藏/取消隐藏各种元素。因此,对于某些单元格,我有一个textview,而对于其他单元格,我有一个imageview。我在viewholder中指定了每个可能的元素,但对于每个单元格,只有某些元素是可见的
private class ViewHolder {
ImageView team_icon;
TextView player_name;
RelativeLayout layout;
TextView team_name;
}