我是 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
像这样我得到。