Domanda

I am having some serious trouble with getView in a ListView. The problem is, that I have CheckBoxes on each row. I need to work with them - the ListView is part of TabHost and I need to remember which CheckBox was checked and use it in other tabs.

However, I also have a "master checkbox" that checks all of the checkboxes in the listview. No big deal, but the thing is, that I just can't find out where I clicked to change the state of the CheckBox in ArrayList, where I keep all the states of the CheckBoxes. Everytime I assign listener to every View, the "int position" as an argument gives some weird number, definitely not the one I need - the position in the adapter, I bet it's something like "position on the screen right now..."

Here is the code:

public View getView(int position, View convertView, ViewGroup parent) {
        count = position;
        View rowView = convertView;
        SingleCarView sView = null;

        if (rowView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.activity_overview_listview_row, null);
            sView = new SingleCarView();

            sView.cbx = (CheckBox)rowView.findViewById(R.id.overview_listview_chckbox);
            sView.carName = (TextView)rowView.findViewById(R.id.overview_listview_label);
            sView.state = (ImageView)rowView.findViewById(R.id.overview_listview_icon);
            rowView.setTag(sView);
        } else {
            sView = (SingleCarView) rowView.getTag();
        }
        // THE TROUBLE BEGINS HERE...
        sView.cbx.setChecked(savedState.get(position));
        sView.cbx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(computerActionWithCheckBox) {
                    // nothing
                } else {
                    if(allCars.isChecked()) {
                        savedState.set(count, isChecked);
                        userModifiedCheckBox = true;
                        allCars.setChecked(false);

                        adapter.notifyDataSetChanged();
                    } else {
                        savedState.set(count, isChecked);
                    }
                }
            }
        });
        sView.carName.setText(objects.get(position));
        switch(DataLoaderBeta.par.carState.get(position)){
        case 1:
            sView.state.setImageResource(R.drawable.ic_moving);
            break;
        case 2:
            sView.state.setImageResource(R.drawable.ic_out_of_signal);
            break;
        case 0:
            sView.state.setImageResource(R.drawable.ic_parked);
            break;
        }
        return rowView;
    }

Thanks a lot for any answer! :-)

È stato utile?

Soluzione

In the getView() method set as a tag for your CheckBox and integer representing the position you get from the getView() method:

sView.cbx.setTag(new Integer(position));

Then in your OnCheckedChangeListener you get the checkbox that was clicked , the buttonView parameter, and from this button get the tag getTag() and use that number for the position in the savedState:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          Integer realPosition = (Integer) buttonView.getTag(); //realPosition will be the real position you get in the getView() method
//other stuff here
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top