返回并绑定到列表视图的双精度数被截断

发布于 2024-11-02 07:59:47 字数 1728 浏览 5 评论 0原文

我正在查询我的 Android 应用程序的 sqlite 数据库。

然后,我使用一个简单的游标适配器将数据绑定到列表视图。

从数据库中调用了两个双打,并且在列表视图中唯一显示为 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>

I am query my sqlite data base for my Android app.

I then use a simple cursor adapter to bind the data to a list view.

there are two doubles being called from the database and on the list view the only show up as 6 digits long.

Example: 43.6234 and -116.203

in the database (I dumped it) these numbers have a lot more digits.

What went wrong?
Here is my row XML for the list view

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴随着你 2024-11-09 07:59:47

查看此处后: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");
                    }
                }
            }
        }
    }
}

After looking here: krysanify.wordpress.com/2010/11/24/… I have found a flaw in the SimpleCursorAdapter that just assumes the fields will be strings loosing the precision wanted

I extended the SimpleCursorAdapter to use Double on the rows I wanted here is the class.

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");
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文