Conditionally formating a ListView. Function only works from OnListItemClick(), but not from OnCreate()?

225 views Asked by At

So I have a nice little list with a cursoradapter.

After creation, I'm trying to color the rows depending on what kind of values they hold. I do it with this function below. The problem:

If I call this function from a "list.setOnItemClickListener()...", it runs perfectly.
But if I call it from my OnCreate(), I get a nullpointerexeption on my "TextView tv = (TextView) childview.findViewById(R.id.row_eszkoz_leltarozott_allapot);" row.

What causes this?

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 {
        }
    }
}
1

There are 1 answers

0
Diogo Bento On BEST ANSWER

In the CursorAdapter override 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;
        }
 }

Btw, System.out.println() ????

check this: http://developer.android.com/reference/android/util/Log.html