Hello,

i update a value in my Database in a Listview using a Context Menu.
The code so far seems to be ok. But everything i try to make the
update visible in the View failed. Anyone can point me whats wrong
there ? I use a ViewBinder to map the Layout for each row.

Row Layout:

?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android";
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


        <TextView
                android:id="@+id/custom_spotoverview_name"
                android:text="text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp" />

        <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android";
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <ImageView
                        android:id="@+id/custom_spotoverview_activ"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                <View
                        android:layout_width="5dp"
                        android:layout_height="5dp" />


                <TextView
                        android:id="@+id/custom_spotoverview_wind_details"
                        android:text="some default text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                <View
                        android:layout_width="5dp"
                        android:layout_height="5dp" />



                <TextView
                        android:id="@+id/custom_spotoverview_detail"
                        android:text="some default text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                <View
                        android:layout_width="5dp"
                        android:layout_height="5dp" />

                <ImageView
                        android:id="@+id/custom_spotoverview_wind_from"
                        android:background="@drawable/icon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                <View
                        android:layout_width="5dp"
                        android:layout_height="5dp" />

                <ImageView
                        android:id="@+id/custom_spotoverview_wind_to"
                        android:background="@drawable/icon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        </LinearLayout>
</LinearLayout>


The ViewBinder:

public class SpotOverviewViewBinder implements
SimpleCursorAdapter.ViewBinder
{

        private static final int SPOT_NAME = 0;
        private static final int SPOT_ID = 2;

        private static final int WIND_TO = 5;
        private static final int WIND_FROM = 6;

        private static final int SPOT_ACTIV = 3;

        /**
         * Converts integer representing a boolean (0,1) to enabled or
disabled icon
         * resource id.
         *
         * @param value
         * @return
         */
        private final static int convertIntToResourceID(final int value)
        {
                return value == 0 ? R.drawable.activ_off : R.drawable.activ_on;
        }

        /*
         * (non-Javadoc)
         *
         * @see
         * android.widget.SimpleCursorAdapter.ViewBinder#setViewValue
(android.view
         * .View, android.database.Cursor, int)
         */
        @Override
        public boolean setViewValue(final View view, final Cursor cursor,
final int columnIndex)
        {
                if (columnIndex == SPOT_ACTIV)
                {
                        final ImageView iv = (ImageView) view;
                        final int resID = convertIntToResourceID(cursor.getInt
(columnIndex));
                        iv.setBackgroundResource(resID);
                }
                else if (columnIndex == SPOT_NAME)
                {
                        final String name = cursor.getString(columnIndex);
                        final TextView tv = (TextView) view;
                        tv.setText(name);
                }
                else if (columnIndex == SPOT_ID)
                {
                        final TextView tv = (TextView) view;
                        tv.setText(cursor.getString(columnIndex));
                }
                else if (columnIndex == WIND_FROM)
                {
                        final ImageView tv = (ImageView) view;
                        final String id = cursor.getString(columnIndex);
                        final int index = IdentityUtil.indexOf(id, 
WindDirection.values());
                        final int resID = 
WindDirection.values()[index].getImage();
                        tv.setImageResource(resID);
                }
                else if (columnIndex == WIND_TO)
                {
                        final ImageView tv = (ImageView) view;
                        final String id = cursor.getString(columnIndex);
                        final int index = IdentityUtil.indexOf(id, 
WindDirection.values());
                        final int resID = 
WindDirection.values()[index].getImage();
                        tv.setImageResource(resID);
                }

                return true;
        }
}

The ListActivity:


public class SpotOverview extends ListActivity
{

        private final static String LOG_TAG = SpotOverview.class.getSimpleName
();
        private static final int ENABLE_ITEM_ID = 0;
        private static final int DISABLE_ITEM_ID = 1;
        private static final int EDIT_ITEM_ID = 2;

        Cursor c = null;
        SimpleCursorAdapter shows = null;

        /*
         * (non-Javadoc)
         *
         * @see android.app.Activity#onCreate(android.os.Bundle)
         */
        @Override
        protected void onCreate(final Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.spotoverview);

                final ISelectedDAO dao = DAOFactory.getSelectedDAO(this);
                c = dao.fetchAll();
                startManagingCursor(c);

                final String[] from = new String[]
                { ISelectedDAO.COLUMN_ACTIV, ISelectedDAO.COLUMN_NAME,
ISelectedDAO.COLUMN_ID, ISelectedDAO.COLUMN_STARTING,
                                ISelectedDAO.COLUMN_TILL };
                final int[] to = new int[]
                { R.id.custom_spotoverview_activ, R.id.custom_spotoverview_name,
R.id.custom_spotoverview_detail,
                                R.id.custom_spotoverview_wind_from,
R.id.custom_spotoverview_wind_to };
                shows = new SimpleCursorAdapter(this,
R.layout.custom_listview_spotoverview, c, from, to);
                shows.setViewBinder(new SpotOverviewViewBinder());
                setListAdapter(shows);

                //
                getListView().setOnCreateContextMenuListener(new
OnCreateContextMenuListener()
                {
                        @Override
                        public final void onCreateContextMenu(final ContextMenu 
menu, final
View v, final ContextMenuInfo menuInfo)
                        {
                                final long selectedItemID = getSelectedItemId();
                                final boolean isActiv = 
dao.isActiv(selectedItemID);
                                menu.add(0, EDIT_ITEM_ID, 0, 
R.string.spot_overview_spot_edit);
                                if (isActiv)
                                {
                                        menu.add(0, DISABLE_ITEM_ID, 0,
R.string.spot_overview_spot_monitoring_disable);
                                }
                                else
                                {
                                        menu.add(0, ENABLE_ITEM_ID, 0,
R.string.spot_overview_spot_monitoring_enable);
                                }
                        }
                });
        }

        /*
         * (non-Javadoc)
         *
         * @see android.app.Activity#onContextItemSelected
(android.view.MenuItem)
         */
        @Override
        public boolean onContextItemSelected(final MenuItem __item)
        {
                switch (__item.getItemId())
                {
                case DISABLE_ITEM_ID:
                        setActive(false);
                        break;
                case ENABLE_ITEM_ID:
                        setActive(true);
                        break;
                case EDIT_ITEM_ID:
                        editSpot();
                        break;
                default:
                        throw new IllegalArgumentException("Unkown Item ID " +
__item.getItemId());
                }

                return true;
        }

        private void setActive(final boolean _state)
        {
                final ISelectedDAO dao = DAOFactory.getSelectedDAO(this);
                dao.setActiv(getSelectedItemId(), _state);
                shows.notifyDataSetChanged();
                Log.d(LOG_TAG, "Updating View");
                // getListView().invalidateViews();
                //
                // final BaseAdapter adapter = (BaseAdapter) getListAdapter();
                // adapter.notifyDataSetChanged();
        }

        private void editSpot()
        {
        }

}

The Database Table:

CREATE TABLE IF NOT EXISTS selected (_id INTEGER PRIMARY KEY
AUTOINCREMENT, spotid text NOT NULL, name TEXT NOT NULL, activ
BOOLEAN, usedirection BOOLEAN, starting TEXT, till TEXT, windmeasure
TEXT NOT NULL, minwind INTEGER, maxwind INTEGER);



The Method setActive will be called after the User selected to enable
or disable an entry. The value is updated in the Database, but what to
do to refresh the View correctly ? I read a lot about
notifyDataSetChanged(); but it does not work, any help is welcome.

Thx,
Jens


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to