Возвращенные двойники и привязанные к списку усечены - PullRequest
1 голос
/ 16 апреля 2011

Я запрашиваю базу данных sqlite для моего приложения для Android.

Затем я использую простой адаптер курсора для привязки данных к представлению списка.

из базы данных вызывается два двойных символа, и в представлении списка отображается только 6 цифр.

Пример: 43.6234 и -116.203

в базе данных (я ее сбросил) эти цифры имеют намного больше цифр.

Что пошло не так? Вот моя строка XML для представления списка

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView android:text="@string/ssid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/row_db_ssid" android:textSize="16dip"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_below="@+id/row_db_bssid" android:textSize="12dip" android:text="@string/lati" android:layout_width="wrap_content" android:id="@+id/row_db_latitude"></TextView>
        <TextView android:text="@string/bssid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/row_db_bssid" android:layout_below="@+id/row_db_ssid" android:textSize="14dip"></TextView>
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="@string/rssi" android:textSize="16dip" android:id="@+id/row_db_rssi"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/row_db_bssid" android:textSize="12dip" android:text="@string/longi" android:layout_width="wrap_content" android:id="@+id/row_db_longitude"></TextView>
    </RelativeLayout>

1 Ответ

0 голосов
/ 17 апреля 2011

После просмотра здесь: krysanify.wordpress.com/2010/11/24/… Я обнаружил недостаток в SimpleCursorAdapter, который просто предполагает, что поля будут строкой, теряющей требуемую точность

Я расширил SimpleCursorAdapterиспользовать Double в строках, которые я хотел здесь - это класс.

public class WithLongAdapter extends SimpleCursorAdapter {
    private int[] mFrom;
    private int[] mTo;
/**
 * SimpleCursorAdapter that also checks for Double values based on rows so they will not be truncated
 * @param context pass this
 * @param layout the row file
 * @param c the Cursors
 * @param from collums on cursor
 * @param to rows on list
 */
    public WithLongAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        mTo = to; //have to make my own to and from because they are protected in SimpleCursorAdapter
        if (c != null) {
            int i;
            int count = from.length;
            if (mFrom == null || mFrom.length != count) {
                mFrom = new int[count];
            }
            for (i = 0; i < count; i++) {
                mFrom[i] = c.getColumnIndexOrThrow(from[i]);
            }
        } else {
            mFrom = null;
        }
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final ViewBinder binder = getViewBinder();
        final int count = mTo.length;
        final int[] from = mFrom;
        final int[] to = mTo;

        for (int i = 0; i < count; i++) {
            final View v = view.findViewById(to[i]);
            if (v != null) {
                boolean bound = false;
                if (binder != null) {
                    bound = binder.setViewValue(v, cursor, from[i]);
                }

                if (!bound) {
                    String text = cursor.getString(from[i]);
                    if (text == null) {
                        text = "";
                    } else if (R.id.row_db_latitude == v.getId()) { //added rows, if they are the ones I want get value of doulbe
                        text = String.valueOf(cursor.getDouble(from[i]));
                    }
                    else if (R.id.row_db_longitude == v.getId()) { //added row
                        text = String.valueOf(cursor.getDouble(from[i]));
                    }
                    if (v instanceof TextView) {
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        setViewImage((ImageView) v, text);
                    } else {
                        throw new IllegalStateException(
                                v.getClass().getName()
                                        + " is not a "
                                        + " view that can be bounds by this SimpleCursorAdapter");
                    }
                }
            }
        }
    }
}
...