无法使用Arrayadapter为listview提供备用颜色

时间:2015-10-29 12:37:11

标签: android android-listview android-arrayadapter

无法使用Arrayadapter为列表视图提供备用颜色

这是我的Logcat ......

10-29 17:56:24.449  18093-18093/com.ambilobes.santom D/AbsListView﹕ Get MotionRecognitionManager
10-29 17:56:24.549  18093-18093/com.ambilobes.santom D/AndroidRuntime﹕ Shutting down VM
10-29 17:56:24.549  18093-18093/com.ambilobes.santom W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40ff0ac8)
10-29 17:56:24.569  18093-18093/com.ambilobes.santom E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.ambilobes.santom.UserAdapter.getView(UserAdapter.java:54)
            at android.widget.AbsListView.obtainView(AbsListView.java:2619)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
            at android.widget.ListView.onMeasure(ListView.java:1165)
            at android.view.View.measure(View.java:16142)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16142)
            at 
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5306)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

我的代码是

    public class UserAdapter extends BaseAdapter{

        Context context;
        ArrayList<Users> userlist;
        List<Users> userlistnew = null;
        public UserAdapter(Context context,  List<Users> userlistnew){
            this.context= context;
            this.userlist = new ArrayList<Users>();
            this.userlistnew= userlistnew;
            this.userlist.addAll(userlistnew);
        }

        @Override
        public int getCount() {
            return userlistnew.size();
        }

        @Override
        public Object getItem(int position) {
            return userlistnew.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        class ViewHolder {
            private TextView textViewUserName;
            private TextView textViewUserId;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = null;

                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.user_item,null);
                viewHolder = new ViewHolder();
                viewHolder.textViewUserName = (TextView) convertView.findViewById(R.id.txtViewUserName);
                viewHolder.textViewUserId = (TextView) convertView.findViewById(R.id.txtViewUserid);
                convertView.setTag(viewHolder);
if(position % 2 == 0){
                    convertView.setBackgroundColor(Color.GRAY);
                }
            Users user = userlistnew.get(position);
            String userName = user.getName();
            String userId = user.getId();
            viewHolder.textViewUserName.setText(userName);
            viewHolder.textViewUserId.setText(userId);
            return convertView;
        }       
    }

无法找到错误。浪费了一天......任何人都可以帮忙。我刚认识android.I想给我的listview提供替代颜色。

3 个答案:

答案 0 :(得分:2)

只需移动您的代码

if(position % 2 == 0){
  convertView.setBackgroundColor(Color.GRAY);
}

convertView = inflater.inflate(R.layout.user_item,null);

答案 1 :(得分:0)

将背景颜色设置为convertView的null变量。因此它不起作用。只需更改代码的位置即可。请在下面这样做。

 if(convertView == null){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.user_item,null);
        viewHolder = new ViewHolder();
        viewHolder.textViewUserName = (TextView) convertView.findViewById(R.id.txtViewUserName);
        viewHolder.textViewUserId = (TextView) convertView.findViewById(R.id.txtViewUserid);
        if(position % 2 == 0){
            convertView.setBackgroundColor(Color.GRAY);
        }
        convertView.setTag(viewHolder);

答案 2 :(得分:0)

您可以尝试以下代码:

if (position % 2 == 1) {

    view.setBackgroundColor(Color.RED);  
} else {
    view.setBackgroundColor(Color.GREEN);  
}

您可以看到此example覆盖arrayadapter。

希望它能帮到你