0

所以我有一个带有光标适配器的漂亮小清单。

创建后,我试图根据它们持有的值对行着色。我用下面的这个函数来做。问题:

如果我从“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 {
        }
    }
}
4

1 回答 1

1

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

于 2013-08-28T14:06:50.217 回答