Custom List item change background in Android
<p>Hi I am using custom listview and my list item contains checkbox. When updating the listview with the existing values the background is changed, so it is working fine.But when i clicks the check button, at that time the background will not changing after loading some where again then the background is changed . My question is at the time of check the item the background need to change immediately.</p>

<p>This is my adapter class.</p>

<pre><code>public class GuestListAdapter extends BaseAdapter implements OnClickListener {
private String strExe;
    AlertDialog.Builder builder;

    Context context;
    private ArrayList&lt;String&gt; arrayListFirstName;
    private ArrayList&lt;String&gt; arrayListLastName;
    private ArrayList&lt;String&gt; arrayListGuests;
    private ArrayList&lt;String&gt; arrayCustomOne;
    private ArrayList&lt;String&gt; arTempId;
    private ArrayList&lt;Boolean&gt; chickinlist;

    public static ArrayList&lt;Integer&gt; arrCheckedItems;
    public static ArrayList&lt;Integer&gt; arrUnCheckedItems;
    Button btnInfo;
    private     SQLiteAdapter mySqliteAdapter;
    private GuestListScreen myGuestList;
    private RelativeLayout views;

    // private AlertDialog alertDialog = null;

    private ArrayList&lt;Boolean&gt; checks = new ArrayList&lt;Boolean&gt;();

    public GuestListAdapter(Context mcontext, ArrayList&lt;String&gt; arrListFN,
            ArrayList&lt;String&gt; arrListLN, ArrayList&lt;String&gt; arrListGuest,
            ArrayList&lt;String&gt; arrTiketID, ArrayList&lt;String&gt; arrCustOne,
            ArrayList&lt;Boolean&gt; chicklist) {

        clearAdapter();

        arrayListFirstName = new ArrayList&lt;String&gt;();
        arrayListLastName = new ArrayList&lt;String&gt;();
        arrayListGuests = new ArrayList&lt;String&gt;();
        arrayCustomOne = new ArrayList&lt;String&gt;();
        arTempId = new ArrayList&lt;String&gt;();
        chickinlist = new ArrayList&lt;Boolean&gt;();
        arrCheckedItems = new ArrayList&lt;Integer&gt;();
        arrUnCheckedItems = new ArrayList&lt;Integer&gt;();

        arrayListFirstName = arrListFN;
        arrayListLastName = arrListLN;
        arrayListGuests = arrListGuest;
        arrayCustomOne = arrCustOne;
        arTempId = arrTiketID;
        chickinlist = chicklist;

        context = mcontext;
        mySqliteAdapter = new SQLiteAdapter(context);

        for (int i = 0; i &lt; arrayListFirstName.size(); i++) {
            checks.add(i, false);
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        Log.d("", "getCount" + arrayListFirstName.size());
        return arrayListFirstName.size();

    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        Log.d("", "getItem" + arrayListFirstName.size());
        return arrayListFirstName.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        final int pos = position;
        views = null;
        LayoutInflater layoutInflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        views = (RelativeLayout) layoutInflator.inflate(
                R.layout.guest_list_item, null);


        final CheckBox chk = (CheckBox) views.getChildAt(0);
//      Log.d("", "CheckBox Pos "+position);
        chk.setId(position);

        TextView txtView = (TextView) views.getChildAt(1);

        TextView txtView2 = (TextView) views.getChildAt(2);
        TextView txtView3 = (TextView) views.getChildAt(3);

        final TextView txtView4 = (TextView) views.getChildAt(4);
        TextView txtView5 = (TextView) views.getChildAt(6);
        txtView5.setId(position);

//      Log.d("", "Button Pos "+position);
        txtView4.setId(position);

        txtView4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, InfoScreen.class);
                intent.putExtra("IDVALUE",arTempId.get(txtView4.getId()) );
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                context.startActivity(intent);
//              System.out.println(v + "##########" + " " + v.getId());
//              System.out.println(v + "##########" + " " + arTempId.get(txtView4.getId()));
//              System.out.println(v + "##########" + " " + chickinlist.get(txtView4.getId()));
            }
        });
         Log.e("", "****************************************************** " );
//       Log.v("", "Adapter arr pos " + pos);
//       Log.v("", "Adapter arr position " + position);
         Log.v("", "Adapter arr size " + arrayListFirstName.size());
         Log.v("", "Passsing arr size " + chickinlist.size());
        for (int dd = 0; dd &lt; arrayListFirstName.size(); dd++) {
            if (position == dd) {
//              Log.d("", "Passsing arr size " + chickinlist.size());
                Boolean result = chickinlist.get(position);
//              Log.d("", "After " + result);
                if (result == true) {
                    chk.setChecked(true);
                    arrCheckedItems.add(position);
                    views.setBackgroundResource(R.drawable.list_item_checked);


                } else {
                    chk.setChecked(false);
                    arrUnCheckedItems.add(position);
                    views.setBackgroundResource(R.drawable.list_item_unchecked);

                }

                txtView.setText(arrayListFirstName.get(position));
                txtView2.setText(arrayListLastName.get(position));
                txtView3.setText("(" + arrayListGuests.get(position) + ")");
                if(arrayCustomOne.get(position).equalsIgnoreCase("0")||arrayCustomOne.get(position).equalsIgnoreCase(null)||arrayCustomOne.get(position).equalsIgnoreCase(""))
                {
                    txtView5.setText("");
                }else
                {
                    txtView5.setText(arrayCustomOne.get(position));
                }

            }

        }

        chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                 strExe = "update ticket_details set checkin_status=1 where tempid="+arTempId.get(chk.getId());
//              Log.d("Adapter", "Checked Temp Id "+arTempId.get(chk.getId()));
                Log.d("", "Position "+chk.getId()+"tempid "+arTempId.get(chk.getId()));
                if (isChecked) {

//                  views.setBackgroundResource(R.drawable.list_item_checked);

                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            context);
                    builder.create();
                    builder.setMessage(arrayListFirstName.get(chk.getId())
                            + " " + arrayListLastName.get(chk.getId())
                            + " has been checked in.");
                    builder.setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
//                                  myGuestList = new GuestListScreen();
                                    mySqliteAdapter.executeCheckQurey(strExe);
//                                  myGuestList.ListUpdate();
                                    dialog.dismiss();
                                }

                            }).show();
//                  Log.d("", "ID = " + buttonView.getId());

                } else {
//                  views.setBackgroundResource(R.drawable.list_item_unchecked);
                     strExe = "update ticket_details set checkin_status=0 where tempid="+arTempId.get(chk.getId());
                        mySqliteAdapter.executeCheckQurey(strExe);


//                  Toast.makeText(context, "check release", Toast.LENGTH_SHORT)
//                          .show();
                }

            }
        });


        return views;

    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        Integer index = (Integer) view.getTag();
        boolean state = checks.get(index.intValue());

        checks.set(index.intValue(), !state);
    }

//  private void showADialog(int posit) {
//
//      AlertDialog.Builder builder = new AlertDialog.Builder(
//              context);
//      builder.create();
//      builder.setMessage("The clicked row is "
//              + arrayListFirstName.get(posit));
//      builder.setPositiveButton("Ok?", new DialogInterface.OnClickListener() {
//
//          @Override
//          public void onClick(DialogInterface dialog, int which) {
//              dialog.dismiss();
//          }
//
//      }).show();
//  }

    public void clearAdapter() {
        if (arrayListFirstName != null) {
            arrayListFirstName.clear();
            arrayListFirstName = null;
            arrayListLastName.clear();
            arrayListLastName = null;
            arrayListGuests.clear();
            arrayListGuests = null;
            arrayCustomOne.clear();
            arrayCustomOne = null;
            arTempId.clear();
            arTempId = null;
            chickinlist.clear();
            chickinlist = null;
            arrCheckedItems.clear();
            arrCheckedItems = null;
            arrUnCheckedItems.clear();
            arrUnCheckedItems = null;
        }

    }

}
</code></pre>

<p>I just want to change the background of the checked item immediatly without updating the list again.</p>
