简体   繁体   中英

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). 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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