I don't fully understand what you mean by "mess" but I had my own mess
with checkboxes. I'm new to android development and was developing
simple checklist that stores the state of items in SQLite. But what
was happening was checkboxes states were going bananas. I watched the
google video on the wonderful world of listview and they give a
minimally adequate understanding of what listview is doing when it
binds. Listview recycles the actual views so that each time it binds a
view may or may not be used for a different item in the list. Where
once an item view is used for data index 1 it may be used for 3 the
next time you update the adapter. My problem was that I was not
setting the appropriate listener to null before I update the checked
states of things in my array.
This is what getview should look like. If convertview is not null then
a recycle view can be used. This is performance enhancement since
inflating is expensive (and part of what leads to the unpredictable
view-item index correspondance)(also virtualizing list has something
to do with confusion and perf as well). After if-else we get items and
make sure to uninitialize handlers. Then set properties and then set
the handlers again.
Not sure if this is what you need or not.
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final GroceryListItem item = getItem(position);
View view = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(_resourceId, null);
} else {
view = convertView;
}
CheckBox checkBox = (CheckBox)
view.findViewById(R.id.itemCheckBox);
TextView textView = (TextView)
view.findViewById(R.id.itemTextView);
ImageButton deleteButton =
(ImageButton)view.findViewById(R.id.itemDeleteButton);
// reset delete button listener
if (deleteButton != null) {
deleteButton.setOnClickListener(null);
}
// reset checkbox listener
if (checkBox != null) {
checkBox.setOnCheckedChangeListener(null);
}
checkBox.setChecked(item.isChecked());
textView.setText(item.getDescription());
// set delete button listener
if (deleteButton != null) {
deleteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
remove(item);
notifyDataSetChanged();
}
});
}
// set checkbox listener
if (checkBox != null) {
checkBox.setOnCheckedChangeListener(new
OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton
buttonView,
boolean isChecked) {
item.setChecked(isChecked);
}
});
}
return view;
}
On Jan 28, 5:07 am, Nick <[email protected]> wrote:
> Hi! I'm trying to do something like task manager for android. Every
> task has it's own params (like time, action, and so on). For now the
> most important param is a task state: disable/enable. I use usual
> checkbox as a state representation. All the tasks are stored in SQLite
> database, so I wrote my CustomCursorAdapter. Then I run into mess.
> What happens every time during scroll???? I debuged the programm and
> saw a lot of bindView calls (in my CustomCursorAdapter). Then I
> googled and saw a descision - using boolean array to store the states
> of my component (example was about CustomArrayAdapter) but... the deal
> is my checkbox change listener disables and enables a corresponding
> task. If I use array - the states are correct but the system calls
> OnCheckedChangeListener.
>
> Next I downloaded google alarm clock (DeskClock) from GIT and saw how
> they'd solved that problem.
> They used DontPressWithParentLayout class what is an extension of
> LinearLayout. Method setPressed is shown below:
> @Override
> public void setPressed(boolean pressed) {
> // If the parent is pressed, do not set to pressed.
> if (pressed && ((View) getParent()).isPressed()) {
> return;
> }
> super.setPressed(pressed);
> }
> And usage:
> <com.android.deskclock.DontPressWithParentLayout android:id="@+id/
> indicator"
> style="@style/alarm_list_left_column"
> android:background="@drawable/clock_selector"
> android:gravity="center"
> android:orientation="vertical">
> <CheckBox android:id="@+id/clock_onoff"
> android:focusable="false"
> android:clickable="false"
> android:background="@drawable/indicator_clock_onoff"
> android:duplicateParentState="true"
> android:layout_height="wrap_content"
> android:layout_width="wrap_content"
> android:layout_gravity="center"
> android:button="@null" />
> <ImageView android:id="@+id/bar_onoff"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:layout_gravity="center"
> android:paddingTop="4dip"
> android:src="@drawable/ic_indicator_off" />
> </com.android.deskclock.DontPressWithParentLayout>
>
> In code (bindView method of AdapteR):
> View indicator = view.findViewById(R.id.indicator);
> // Set the initial resource for the bar image.
> final ImageView barOnOff =
> (ImageView)
> indicator.findViewById(R.id.bar_onoff);
> barOnOff.setImageResource(alarm.enabled ?
> R.drawable.ic_indicator_on :
> R.drawable.ic_indicator_off);
>
> // Set the initial state of the clock "checkbox"
> final CheckBox clockOnOff =
> (CheckBox)
> indicator.findViewById(R.id.clock_onoff);
> clockOnOff.setChecked(alarm.enabled);
>
> // Clicking outside the "checkbox" should also change the
> state.
> indicator.setOnClickListener(new OnClickListener() {
> public void onClick(View v) {
> clockOnOff.toggle();
>
> updateIndicatorAndAlarm(clockOnOff.isChecked(),
> barOnOff, alarm);
> }
> });
>
> ...
> private void updateIndicatorAndAlarm(boolean enabled, ImageView bar,
> Alarm alarm) {
> bar.setImageResource(enabled ? R.drawable.ic_indicator_on
> : R.drawable.ic_indicator_off);
> Alarms.enableAlarm(this, alarm.id, enabled);
> if (enabled) {
> SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
> alarm.daysOfWeek);
> }
> }
>
> I made something like this....but I left checkbox and remove the
> image....and the mess still happens! I just can't believe to work with
> list items is so complicated... Why??? How the hell is to solve that
> problem? HEEEELP!
> Maybe I shoulw develop my own on\off button and put it in my layout
> and override it's onClick method? Or even made it Checkable? But I
> still want to use checkbox!!!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en