如何使用单选按钮更改列表视图项的文本颜色?

时间:2016-09-28 08:28:00

标签: android

我一直在尝试使用单选按钮找到更改特定列表视图项的文本颜色的方法,但我无法弄明白。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您是否将List和ViewHolder用于ListView项目? 如果是这样,只需在适配器中编写更改颜色代码。例如:

private static class ItemHolder {
    TextView name;
}

在适配器中:

public class CompanyAdapter extends ArrayAdapter<CompanyDomain> {

private ArrayList<CompanyDomain> companies;

public CompanyAdapter(Context context, int resource,
        ArrayList<CompanyDomain> items) {
    super(context, resource, items);
    this.companies = items;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    ItemHolder itemHolder;
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        view = inflater.inflate(R.layout.company_list_row, null);
        itemHolder = new ItemHolder();
        itemHolder.name = (TextView) view.findViewById(R.id.companyNameTitle);
    }
    else {
        itemHolder = (ItemHolder) view.getTag();
    }

    // condition here
    if (have to change color) {
        itemHolder.name.setTextColor(Color.RED);
    }

但是现在,最好将RecyclerViewRecyclerView.Adapter

一起使用