简体   繁体   English

为什么我的Android自定义适配器样式随机行?

[英]Why does my Android custom adapter style random rows?

I have a simple custom adapter that I'm using to display a list of degrees. 我有一个简单的自定义适配器,用于显示度数列表。 I'm trying to add a way to make a sort of sub title in the list to group like content together. 我正在尝试添加一种方法来使列表中的子标题具有相似的内容。 Each row has a title and a description, so what I'm trying to do is when it inflates a row that has description set to null, it changes the style of the row (hides the description and centers the title). 每行都有一个标题和描述,所以我要做的是将描述设置为null的行膨胀时,它会更改行的样式(隐藏描述并使标题居中)。 The problem I'm running into is that is applies the style to random rows instead of just the ones with the null value (the title on random rows will be centered and the description on other random rows will be hidden) My getView: 我遇到的问题是将样式应用于随机行,而不是仅将其应用于具有空值的样式(随机行的标题将居中,而其他随机行的描述将被隐藏)我的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;
}

So my question is, why does this style random rows instead of just the ones I tell it to? 所以我的问题是,为什么这种样式会随机排列行而不是我告诉它的行?

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());
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM