如何在android中分类?

时间:2016-06-14 07:05:53

标签: android

我有一个应用程序,我必须将应用程序分为两个高,中,我 想要在中栏中设置高列和中等风险的高风险应用程序我该怎么做,这是我试图实现但不成功的代码

这里是代码: -

public void _fillRowData(int position, View rootView) {
    if (position == _appHeaderIndex || position == _systemMenacesHeaderIndex) {
        ResultsAdapterHeaderItem obj = (ResultsAdapterHeaderItem) getItem(position);
        ResultsAdapterHeaderItem header = (ResultsAdapterHeaderItem) obj;
        TextView headerText = (TextView) rootView.findViewById(R.id.Titlelabel);
        headerText.setText(header.getDescription());
    } else if (_systemMenacesHeaderIndex == -1 || position < _systemMenacesHeaderIndex) //We are receiving something that is not a header and no system menaces
    {
        final ResultsAdapterProblemItem ri = (ResultsAdapterProblemItem) getItem(position);
        final AppProblem ap = ri.getAppProblem();

        TextView textView = (TextView) rootView.findViewById(R.id.Titlelabel);
        TextView riskText = (TextView) rootView.findViewById(R.id.qualityApp);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.logo);
        if (ap.isDangerous()) {
            riskText.setTextColor(ContextCompat.getColor(getContext(), R.color.HighRiskColor));
            riskText.setText(R.string.high_risk);
        } else {
            riskText.setTextColor(ContextCompat.getColor(getContext(), R.color.MediumRiskColor));
            riskText.setText(R.string.medium_risk);
        }

        RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.itemParent);
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (_onItemChangedStateListener != null)
                    _onItemChangedStateListener.onItemSelected(ap);
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (_onItemChangedStateListener != null)
                    _onItemChangedStateListener.onItemSelected(ap);
            }
        });


        textView.setText(StaticTools.getAppNameFromPackage(getContext(), ap.getPackageName()));
        imageView.setImageDrawable(StaticTools.getIconFromPackage(ap.getPackageName(), getContext()));
    } else {
        final ResultsAdapterProblemItem ri = (ResultsAdapterProblemItem) getItem(position);
        final SystemProblem sp = ri.getSystemProblem();

        TextView textView = (TextView) rootView.findViewById(R.id.Titlelabel);
        TextView riskText = (TextView) rootView.findViewById(R.id.qualityApp);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.logo);

        if (sp.isDangerous()) {
            riskText.setTextColor(ContextCompat.getColor(getContext(), R.color.HighRiskColor));
            riskText.setText(R.string.high_risk);
        } else {
            riskText.setTextColor(ContextCompat.getColor(getContext(), R.color.MediumRiskColor));
            riskText.setText(R.string.medium_risk);
        }

        RelativeLayout linearLayout = (RelativeLayout) rootView.findViewById(R.id.itemParent);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (_onItemChangedStateListener != null)
                    _onItemChangedStateListener.onItemSelected(sp);
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (_onItemChangedStateListener != null)
                    _onItemChangedStateListener.onItemSelected(sp);
            }
        });

        textView.setText(sp.getTitle(getContext()));
        imageView.setImageDrawable(sp.getIcon(getContext()));
    }
}

1 个答案:

答案 0 :(得分:0)

您只需在将其设置为适配器

之前对其进行排序
List<ResultsAdapterProblemItem> yourList = ...;
        Collections.sort(yourList, new Comparator<ResultsAdapterProblemItem>() {
            @Override
            public int compare(ResultsAdapterProblemItem lhs, ResultsAdapterProblemItem rhs) {
                if(lhs.getAppProblem().isDangerous()) {
                    if(rhs.getAppProblem().isDangerous()) {
                        return 0;
                    } else {
                        return 1;
                    }
                } else if(rhs.getAppProblem().isDangerous()){
                    return -1;
                } else {
                    return 0;
                }
            }
        });

在此之后,危险物品将位于列表顶部,并且非危险 - 位于底部。现在,您可以将此列表设置为适配器的源