0

我有一个简单的自定义适配器,用于显示度数列表。我正在尝试添加一种方法来在列表中制作一种子标题,以便将类似的内容组合在一起。每行都有一个标题和一个描述,所以我想要做的是当它膨胀一个描述设置为空的行时,它会改变行的样式(隐藏描述并将标题居中)。我遇到的问题是将样式应用于随机行,而不仅仅是具有空值的行(随机行上的标题将居中,其他随机行上的描述将被隐藏)我的 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;
}

所以我的问题是,为什么这种样式是随机行而不是我告诉它的那些?

4

1 回答 1

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());
            }
        }
    }
于 2013-10-31T08:31:53.717 回答