在set viewbinder中更改Relativelayout的背景颜色

时间:2013-08-13 11:44:58

标签: android listview

我是Android新手,在我的应用程序中我想更改自定义列表视图的颜色。这是我的代码

String[] from = new String[] { Contactsnew.CONTENT};
int[] to = new int[] { R.id.content123, R.id.linear123};
custAdapter = new SimpleCursorAdapter(this,
        R.layout.activity_contactitem, null, from, to);
//ListView listView = (ListView) findViewById(R.id.listv);
listview.setAdapter(custAdapter);
    custAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){

    @Override
    public boolean setViewValue(View view, Cursor cursor, int col) {
        // TODO Auto-generated method stub
        Log.d("asda", "asd");
        if(view.getId() == R.id.linear123){
        RelativeLayout relative = (RelativeLayout)view.findViewById(R.id.linear123);
        relative.setBackgroundColor(0xFFC1E7F);
        return true;
        }
        return false;
    }

});

运行后显示一个ArrayIndexoutofbounds。

日志报告

08-13 12:44:57.004: E/AndroidRuntime(2107): FATAL EXCEPTION: main 
08-13 12:44:57.004: E/AndroidRuntime(2107): java.lang.ArrayIndexOutOfBoundsException
08-13 12:44:57.004: E/AndroidRuntime(2107):     at        android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:131)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.fillDown(ListView.java:652)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.fillFromTop(ListView.java:709)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.layoutChildren(ListView.java:1566)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ScrollView.onLayout(ScrollView.java:1205)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at  android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at 

像我这样。

2 个答案:

答案 0 :(得分:0)

这是因为变量“relative”为null。你有设置当前视图吗?如果不是 - >通过ID

获取视图时,您将获得NPE

答案 1 :(得分:0)

您在错误的视图上运行命令findViewById,您要做的是在列表视图中找到相对布局(当然,它不会在那里找到它,这就是为什么你得到 null ) findViewById仅在激活的对象(在您的情况下为listview)下搜索视图。

您需要在包含findViewById

的布局上运行RelativeLayout

如果您在片段中运行,请尝试getView().findViewById(...)