有条件地格式化ListView.函数只能在OnListItemClick()中工作,但不能在OnCreate()中使用



所以我有一个很好的带有游标适配器的小列表。

在创建之后,我尝试根据行所包含的值来为行着色。我用下面的这个函数来做。问题:

如果我从"list.setOnItemClickListener()…"调用此函数,它将完美运行
但是,如果我从OnCreate()调用它,我会在"TextView tv=(TextView)childview.findViewById(R.id.row_eszkoz_leltarozott_allapot);"行上得到nullpointerexeption

是什么原因导致的

public void ConditionalColoring()
{
    for (int position=0; position<adapter.getCount(); position++)
    {
        System.out.println("adapter child szám: " + adapter.getCount());
        View childview = list.getChildAt(position);
        //View childview = adapter.getView(position, null , list);
        TextView tv = (TextView) childview.findViewById(R.id.row_eszkoz_leltarozott_allapot);   ///ERROR HERE
        RelativeLayout RL = (RelativeLayout) childview.findViewById(R.id.row_eszkoz_container);
        String s = (String) tv.getText();
        System.out.println("szöveg: " + s);
        if (s.equals("leltárazva")) {
            int holoblue = activity.getResources().getColor(android.R.color.holo_blue_light);
            RL.getBackground().setColorFilter(holoblue,PorterDuff.Mode.MULTIPLY);
        }
        else if (s.equals("leltározandó")) {
            int hologreen = activity.getResources().getColor(android.R.color.holo_blue_light);
            RL.getBackground().setColorFilter(hologreen,PorterDuff.Mode.MULTIPLY);
        }
        else if (s.equals("módosítási tranzakció szükséges")) {
            int holored = activity.getResources().getColor(android.R.color.holo_blue_light);
            RL.getBackground().setColorFilter(holored,PorterDuff.Mode.MULTIPLY);
        } 
        else {
        }
    }
}

CursorAdapter中覆盖getView()

new SimpleCursorAdapter(getActivity(),
                        R.layout.rowlayout,
                        null (opt cursor),
                        fromColumns,
                        toLayout, 0) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
            row.findViewByID(...)
                            ...enter code here...
            return row;
        }
 }

顺便说一下,System.out.println()????

检查此项:http://developer.android.com/reference/android/util/Log.html

相关内容

最新更新