为什么我的convertView == null值,getView方法? [英] why is my convertView == null on getView method?
问题描述
我有Viewlist产品+定制适配器一个活动。
我要更新运行时Viewlist产品(使用回调时,一些资源完成下载)。我所说的UI线程调用此方法:
公共无效refreshListIcons(){ 如果(类!= NULL){ SettingsValue []值= adapter.getValues();
的for(int i = 0; I< values.length;我++){
值[I] .icon = ResManager
.GetSkinDrawable(类别[I] .iconName +.BIN);
} adapter.setValues(值); mListView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}
然后 adapter.getView
方法被称为:
但为什么 convertView == NULL
一个第一次活动是创建我明白了。
但是,现在这是我第二次(运行时更新)。
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
如果(convertView == NULL){
convertView = inflater.inflate(R.layout.settings_item,NULL);
}
...
}
这会导致重新充气太多次崩溃和我的应用程序。
我的整个适配器的code:
包com.waze.settings;
公共类SettingValueAdapter延伸BaseAdapter { 私人SettingsValue []值;
私人LayoutInflater吹气;
公共SettingValueAdapter(上下文的背景下){
充气= LayoutInflater.from(上下文);
} 公共SettingsValue []的GetValues(){
返回值;
} @覆盖
公众诠释的getCount(){
如果(价值== NULL){
返回0;
}
返回values.length;
}
@覆盖
公共对象的getItem(INT为arg0){
返回值== NULL?空:值[为arg0]
} @覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回0;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
如果(convertView == NULL){
convertView = inflater.inflate(R.layout.settings_item,NULL);
}
SettingsValue项=值[位置]
CheckedTextView名=(CheckedTextView)convertView.findViewById(R.id.itemText);
ImageView的ICONVIEW =(ImageView的)convertView.findViewById(R.id.itemIcon);
如果(ICONVIEW = NULL&放大器;!及(项目= NULL)及!&安培;!(item.icon = NULL)){
iconView.setImageDrawable(item.icon);
iconView.setVisibility(View.VISIBLE);
}其他{
iconView.setVisibility(View.GONE);
}
name.setText(item.display);
name.setChecked(item.isSelected);
View容器= convertView.findViewById(R.id.itemContainer);
如果(位置== 0){
如果(位置== values.length-1){
container.setBackgroundResource(R.drawable.item_selector_single);
}其他{
container.setBackgroundResource(R.drawable.item_selector_top);
}
}其他{
如果(位置== values.length-1){
container.setBackgroundResource(R.drawable.item_selector_bottom);
}其他{
container.setBackgroundResource(R.drawable.item_selector_middle);
}
}
container.setPadding(0,0,0,0);
返回convertView;
} 公共无效setValues方法(SettingsValue []值){
this.values =值;
notifyDataSetChanged();
}
}
我想你不应该重新设置适配器。相反,修改您的刷新方法如下:
公共无效refreshListIcons(){ 如果(类!= NULL){ SettingsValue []值= adapter.getValues();
的for(int i = 0; I< values.length;我++){
值[I] .icon = ResManager
.GetSkinDrawable(类别[I] .iconName +.BIN);
} adapter.notifyDataSetChanged();
/ *
adapter.setValues(值);
mListView.setAdapter(适配器);
adapter.notifyDataSetChanged(); * /
}
}
另外,在适配器中添加viewHolder类。
I have an Activity with viewList + Custom Adapter.
I want to update the viewList on runtime (using callback when some resources are done downloading). I call the ui-thread to invoke this method:
public void refreshListIcons() {
if (categories != null) {
SettingsValue[] values = adapter.getValues();
for (int i = 0; i < values.length; i++) {
values[i].icon = ResManager
.GetSkinDrawable(categories[i].iconName + ".bin");
}
adapter.setValues(values);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
and then the adapter.getView
method is called:
but why is convertView==null
?
one the first time the activity is created I understand.
But now this is my second time (runtime update).
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.settings_item, null);
}
...
}
It causes re-inflating too many times and crashes my app.
my entire adapter's code:
package com.waze.settings;
public class SettingValueAdapter extends BaseAdapter {
private SettingsValue[] values;
private LayoutInflater inflater;
public SettingValueAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public SettingsValue[] getValues() {
return values;
}
@Override
public int getCount() {
if (values == null) {
return 0;
}
return values.length;
}
@Override
public Object getItem(int arg0) {
return values==null? null : values[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.settings_item, null);
}
SettingsValue item = values[position];
CheckedTextView name = (CheckedTextView) convertView.findViewById(R.id.itemText);
ImageView iconView = (ImageView) convertView.findViewById(R.id.itemIcon);
if (iconView != null && (item != null) && (item.icon != null)) {
iconView.setImageDrawable(item.icon);
iconView.setVisibility(View.VISIBLE);
} else {
iconView.setVisibility(View.GONE);
}
name.setText(item.display);
name.setChecked(item.isSelected);
View container = convertView.findViewById(R.id.itemContainer);
if (position == 0) {
if (position == values.length-1) {
container.setBackgroundResource(R.drawable.item_selector_single);
} else {
container.setBackgroundResource(R.drawable.item_selector_top);
}
} else {
if (position == values.length-1) {
container.setBackgroundResource(R.drawable.item_selector_bottom);
} else {
container.setBackgroundResource(R.drawable.item_selector_middle);
}
}
container.setPadding(0, 0, 0, 0);
return convertView;
}
public void setValues(SettingsValue[] values) {
this.values = values;
notifyDataSetChanged();
}
}
I guess you shouldn't set the adapter again. Instead modify your refresh method as:
public void refreshListIcons() {
if (categories != null) {
SettingsValue[] values = adapter.getValues();
for (int i = 0; i < values.length; i++) {
values[i].icon = ResManager
.GetSkinDrawable(categories[i].iconName + ".bin");
}
adapter.notifyDataSetChanged();
/*
adapter.setValues(values);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();*/
}
}
Also add a viewHolder class in your adapter.
这篇关于为什么我的convertView == null值,getView方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!