我有一个列表视图,一个自定义的,我想在第一个项目的左侧有一个加号图标。仅在第一项!
我编写了自定义适配器并在 getView() 方法中使用了这样的 if 条件:
If position == 0
imgicon.setvisibility=0;
}
else
{
imgicon.setvisibility=1;
}
但是当我上下滚动列表时,不仅第一个项目显示图标,而且一些随机项目也显示该图标。怎么了?这是一个错误吗?
编辑:更不用说如果我删除线:imgicon.setvisibility==0
,所有图标都会消失。
编辑:
这是我的自定义数组适配器:
public class Projects_list_custom_array extends ArrayAdapter<Project_Detail> implements
Filterable {
private final Object mLock = new Object();
private ItemsFilter mFilter;
public ArrayList<Project_Detail> mItems;
private ArrayList<Project_Detail> objects;
public Projects_list_custom_array(Context context, int textViewResourceId,
ArrayList<Project_Detail> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
this.mItems = new ArrayList<Project_Detail>(objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_placeholder_projects_list,
null);
}
Project_Detail i = objects.get(position);
if (i != null) {
TextView tt = (TextView) v
.findViewById(R.id.projects_list_first_placeholder);
RelativeLayout projects_list_layout = (RelativeLayout)v.findViewById(R.id.projects_list_placeholder_layout);
if (tt != null) {
tt.setText(i.get_projectname() + " " + position);
}
if (position == 0) {
ImageView imgv = new ImageView(getContext());
imgv.setImageResource(R.drawable.project_add);
projects_list_layout.addView(imgv);
}
}
return v;
}
/* ... */
@Override
public int getCount() {
return objects.size();
}
@Override
public Project_Detail getItem(int position) {
return objects.get(position);
}