0

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.

This is my adapter class.

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

    Context context;
    private ArrayList<String> arrayListFirstName;
    private ArrayList<String> arrayListLastName;
    private ArrayList<String> arrayListGuests;
    private ArrayList<String> arrayCustomOne;
    private ArrayList<String> arTempId;
    private ArrayList<Boolean> chickinlist;

    public static ArrayList<Integer> arrCheckedItems;
    public static ArrayList<Integer> arrUnCheckedItems;
    Button btnInfo;
    private     SQLiteAdapter mySqliteAdapter;
    private GuestListScreen myGuestList;
    private RelativeLayout views;

    // private AlertDialog alertDialog = null;

    private ArrayList<Boolean> checks = new ArrayList<Boolean>();

    public GuestListAdapter(Context mcontext, ArrayList<String> arrListFN,
            ArrayList<String> arrListLN, ArrayList<String> arrListGuest,
            ArrayList<String> arrTiketID, ArrayList<String> arrCustOne,
            ArrayList<Boolean> chicklist) {

        clearAdapter();

        arrayListFirstName = new ArrayList<String>();
        arrayListLastName = new ArrayList<String>();
        arrayListGuests = new ArrayList<String>();
        arrayCustomOne = new ArrayList<String>();
        arTempId = new ArrayList<String>();
        chickinlist = new ArrayList<Boolean>();
        arrCheckedItems = new ArrayList<Integer>();
        arrUnCheckedItems = new ArrayList<Integer>();

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

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

        for (int i = 0; i < 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 < 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;
        }

    }

}

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

4

1 回答 1

0

我认为您需要在调用 OnCheckedChanged 侦听器时告诉 BaseAdapter 刷新数据。

GuestListAdapter.this.notifyDataSetChanged()
于 2012-12-27T10:39:32.490 回答