Question

I have a custom adapter that renders two (2) checkboxes, a picture and the name of the client. All the information needed for the adapter is fetched from an ArratList that contains the Client class.

Every row needs to have both checkboxes checked (selected) for the client in order to process the purchase order, in case that a particular client has one checkbox checked-off and the other checkbox not, that raises a flag as MISMATCH. To make a valid order both checkboxes need to be checked-off.

We are implementing a button for verification, which will find any mismatch in the adapter and then hightlight the mismatches.

EDITION: After pressing the verificationBtn I am able to identify if any row has mismatch on checkboxes, for example, if checkbox1 was checked and checkbox2 not. that will mark the row as mismatch. I am using the position of my checkboxes based on clientList that is an arraylist of List clientList.

QUESTION: How can I get the position that the viewHolder has in order compare against the clientList position? Is there any way I can force the viewHolder to store the position and get it back and make the comparison with cli.getClient_number() ?

So far I have tested two different ways with no luck:

Method 1:

 viewHolder.clientName.setBackgroundColor((Interger.parseInt(cli.getClient_number()) ) == position ? Color.GREEN : Color.TRANSPARENT);

Method 2

viewHolder.clientName.setBackgroundColor(Color.GREEN);

here the code that I am implementing.

// This goes in my main Client Activity
Button verificationBtn = (Button) findViewById(R.id.verificationBtn);
    verificationBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

                buffer.setLength(0);
                mismatchTv.setText("");

                for (Client cli : clientList) {
                    if (cli.isCheckboxOneSelected() != cli.isCheckboxTwoSelected()) {

                    //We had defined above the following buffer = new StringBuffer();
                    buffer.append((ah.parseInt(cli.getClient_number(), 0) - 1) + ", ");
                    cli.setMismatch(true);

                    //We are passing here the ID that correspond to the client mismatch
                    list_adapter.setBackgroundColor(Integer.parseInt(cli.getClient_number()) - 1);

                    setListAdapter(list_adapter);
                    Log.w("cli.getClient_number() ", String.valueOf(Integer.parseInt(cli.getClient_number()) - 1));
                }


}

// We display any mismatch on a TextView on top of the screen
if (buffer.length() != 0) {

    //This is a TextView on top of the screen
    mismatchTv.setText("Error en Client(s) "
        + buffer.toString());
}




// This goes inside of the ClientArrayAdapter
public void setBackgroundColor(int position) {
    Log.w("inside of setBackgroundColor method", "True");
    switchIndex = 1;
    positionFetched = position;
}

// This goes inside of the ClientArrayAdapter
// and inside the body of public View getView(int position, View convertView, ViewGroup parent) {   
switch (switchIndex) {

    case 1:

        viewHolder.cbone
            .setButtonDrawable(R.drawable.btn_checkbox_selector);
        viewHolder.cbtwo
            .setButtonDrawable(R.drawable.btn_checkbox_selector);

        Log.w("switch 1 was called ", "True");
        for (Client cli : clientList) {
            if (cli.isCheckboxOneSelected() != cli.isCheckboxTwoSelected()) {

                Client cli = getItem(positionFetched);

                    if (cli.isMismatch()) { 
                        cli.setColor(Color.BLACK); 
                        Log.e("if (cli.isMismatch()) ", "");

                        //HERE WE ARE TRYING TO HIGHLIGHT THE ROW WITH MISMATCH
                        //WHY THIS LINE DOES NOT WORK?
                        //THE ISSUE THAT I AM GETTING IS THAT I CANNOT CONTROL  WHAT ROW TO AFFECT 
                        //IN THE VIEW HOLDER
                        viewHolder.clientName.setBackgroundColor(Color.GREEN);

                    }

                }

            }

        break;

    default:
        viewHolder.cbone.setButtonDrawable(R.drawable.disabled_cb);
        viewHolder.cbtwo.setButtonDrawable(R.drawable.disabled_cb);
        break;
}

// This goes inside of the ClientArrayAdapter           
    static class ViewHolder {
        public TextView clientName;
        public TextView clientNumber;
        public ImageView imageView;
        public CheckBox cbtwo;
        public CheckBox cbone;
        public int position;
    }
Was it helpful?

Solution

After three days trying to figure out what's wrong with this code, I finally found the solution moving the Method #1 just at the very end of the getView method. :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top