我有一个简单的自定义适配器,我用它来显示度数列表。我正在尝试添加一种方法,在列表中创建一种子标题,将内容组合在一起。每行都有一个标题和一个描述,所以我要做的是当它将描述设置为null的行膨胀时,它会改变行的样式(隐藏描述并使标题居中)。我遇到的问题是将样式应用于随机行而不仅仅是具有空值的行(随机行上的标题将居中,其他随机行的描述将被隐藏) 我的getView:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.links_list, null);
}
TextView tvTitle = (TextView)v.findViewById(R.id.txtTitle);
TextView tvDesc = (TextView)v.findViewById(R.id.txtDesc);
if(mainList.get(position) instanceof Program) {
Program row = (Program) mainList.get(position);
if(row.getTitle() != null && row.getType() == null) {
//this condition should style just the ones with null type, but style random rows instead
tvTitle.setText(row.getTitle());
tvTitle.setGravity(Gravity.CENTER);
tvDesc.setVisibility(View.GONE);
} else {
if(row.getTitle() != null) {
tvTitle.setText(row.getTitle());
}
if(row.getType() != null) {
tvDesc.setText(row.getType());
}
}
}
return v;
}
所以我的问题是,为什么这样会随机排列而不是我告诉它的那些?
答案 0 :(得分:1)
if(mainList.get(position) instanceof Program) {
Program row = (Program) mainList.get(position);
if(row.getTitle() != null && row.getType() == null) {
//this condition should style just the ones with null type, but style random rows instead
tvTitle.setVisibility(View.VISIBLE);
tvTitle.setText(row.getTitle());
tvTitle.setGravity(Gravity.CENTER);
tvDesc.setVisibility(View.GONE);
} else {
tvTitle.setVisibility(View.GONE);
tvTitle.setGravity(Gravity.LEFT);
tvDesc.setVisibility(View.VISIBLE);
if(row.getTitle() != null) {
tvTitle.setText(row.getTitle());
}
if(row.getType() != null) {
tvDesc.setText(row.getType());
}
}
}