// getView method is called for each item of ListView public View getView(final int position, View convertView, ViewGroup parent) { // inflate the layout for each item of listView (our services) ViewHolderItem vH; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.kali_services_item, parent, false); // set up the ViewHolder vH = new ViewHolderItem(); // get the reference of switch and the text view vH.swTitle = (TextView) convertView.findViewById(R.id.switchTitle); vH.sw = (Switch) convertView.findViewById(R.id.switch1); vH.swholder = (TextView) convertView.findViewById(R.id.switchHolder); vH.swBootCheckbox = (CheckBox) convertView.findViewById(R.id.initAtBoot); convertView.setTag(vH); // System.out.println ("created row"); } else { // recycle the items in the list is already exists vH = (ViewHolderItem) convertView.getTag(); } if (position >= _serviceStates.length) { // out of range, return ,do nothing return convertView; } // remove listeners vH.sw.setOnCheckedChangeListener(null); vH.swBootCheckbox.setOnCheckedChangeListener(null); // set service name vH.swTitle.setText(services[position][0]); // clear state vH.sw.setChecked(false); vH.swBootCheckbox.setChecked(false); // check it // running services if (_serviceStates[position].equals("1")) { vH.sw.setChecked(true); vH.swTitle.setTextColor(mContext.getResources().getColor(R.color.blue)); vH.swholder.setText(services[position][0] + " Service is UP"); vH.swholder.setTextColor(mContext.getResources().getColor(R.color.blue)); } else { vH.sw.setChecked(false); vH.swTitle.setTextColor(mContext.getResources().getColor(R.color.clearTitle)); vH.swholder.setText(services[position][0] + " Service is DOWN"); vH.swholder.setTextColor(mContext.getResources().getColor(R.color.clearText)); } // services enabled at boot if (_serviceBootStates[position].equals("1")) { // is enabled vH.swBootCheckbox.setChecked(true); vH.swBootCheckbox.setTextColor(mContext.getResources().getColor(R.color.blue)); } else { // is not :) vH.swBootCheckbox.setChecked(false); vH.swBootCheckbox.setTextColor(mContext.getResources().getColor(R.color.clearTitle)); } // add listeners final ViewHolderItem finalVH = vH; vH.sw.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { new Thread( new Runnable() { public void run() { exe.RunAsRoot(new String[] {services[position][2]}); } }) .start(); _serviceStates[position] = "1"; finalVH.swholder.setText(services[position][0] + " Service Started"); finalVH.swTitle.setTextColor(mContext.getResources().getColor(R.color.blue)); finalVH.swholder.setTextColor(mContext.getResources().getColor(R.color.blue)); } else { new Thread( new Runnable() { public void run() { exe.RunAsRoot(new String[] {services[position][3]}); } }) .start(); _serviceStates[position] = "0"; finalVH.swholder.setText(services[position][0] + " Service Stopped"); finalVH.swTitle.setTextColor(mContext.getResources().getColor(R.color.clearTitle)); finalVH.swholder.setTextColor(mContext.getResources().getColor(R.color.clearText)); } } }); vH.swBootCheckbox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { new Thread( new Runnable() { public void run() { Log.d("bootservice", "ADD " + services[position][4]); addBootService(position); } }) .start(); _serviceBootStates[position] = "1"; finalVH.swBootCheckbox.setTextColor(mContext.getResources().getColor(R.color.blue)); } else { new Thread( new Runnable() { public void run() { Log.d("bootservice", "REMOVE " + services[position][4]); removeBootService(position); } }) .start(); _serviceBootStates[position] = "0"; finalVH.swBootCheckbox.setTextColor( mContext.getResources().getColor(R.color.clearTitle)); } } }); return convertView; }