SQLiteDatabase 如何在 SQLiteDatabase 查询和 SimpleCursorAdapter 中连接两个字符串?

发布于 2024-12-05 16:11:06 字数 901 浏览 1 评论 0原文

表字段:

_id、街道、house_nr

查询:

mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_STREET_NAME,
                KEY_HOUSE_NUMBER}, null, null, null, null, KEY_ROWID + " DESC");

SimpleCursorAdapter:

    Cursor c = mDbHelper.fetchAllRows();
    startManagingCursor(c);

    String[] from = new String[] { HistoryDbAdapter.KEY_STREET_NAME};
    int[] to = new int[] { R.id.text1 };

    SimpleCursorAdapter entries =
        new SimpleCursorAdapter(this, R.layout.history_row, c, from, to);
    setListAdapter(entries);

如何将两个数据库字段中的一个组合字符串添加到 R.id.text1 中显示完整地址。

示例:

street = "Android street"
house_nr = "911"

然后:

R.id.text1 = "Android street 911"

谢谢

Table fields:

_id, street, house_nr

query:

mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_STREET_NAME,
                KEY_HOUSE_NUMBER}, null, null, null, null, KEY_ROWID + " DESC");

SimpleCursorAdapter:

    Cursor c = mDbHelper.fetchAllRows();
    startManagingCursor(c);

    String[] from = new String[] { HistoryDbAdapter.KEY_STREET_NAME};
    int[] to = new int[] { R.id.text1 };

    SimpleCursorAdapter entries =
        new SimpleCursorAdapter(this, R.layout.history_row, c, from, to);
    setListAdapter(entries);

How to add one combined string from two db fields into R.id.text1 to show full address.

example:

street = "Android street"
house_nr = "911"

then:

R.id.text1 = "Android street 911"

thanks

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

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

发布评论

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

评论(2

尴尬癌患者 2024-12-12 16:11:06

有两种方法:

  • 连接查询中的两个列名称:KEY_STREET_NAME + " || " + KEY_HOUSE_NUMBER。然后我将使用 rawQuery。

  • 使用自定义适配器。

There are two ways:

  • Concat both column names in the query: KEY_STREET_NAME + " || " + KEY_HOUSE_NUMBER. I would use a rawQuery then.

  • Use a custom adapter.

同展鸳鸯锦 2024-12-12 16:11:06

它有效:

public class HistoryCursorAdapter extends SimpleCursorAdapter {
    private final LayoutInflater mInflater;

    public HistoryCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.history_row, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text1);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Cursor c = getCursor();
        c.moveToPosition(position);
        int street_index = c.getColumnIndex(HistoryDbAdapter.KEY_STREET_NAME);
        int house_nr_index = c.getColumnIndex(HistoryDbAdapter.KEY_HOUSE_NUMBER);
        holder.text.setText(String.format("%s %s", c.getString(street_index), c.getString(house_nr_index)));
        return convertView;

    }

    static class ViewHolder {
        TextView text;
    }
}

那是我第一次。也许可以写得更简单一些?也许有任何速度建议?

It works:

public class HistoryCursorAdapter extends SimpleCursorAdapter {
    private final LayoutInflater mInflater;

    public HistoryCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.history_row, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text1);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Cursor c = getCursor();
        c.moveToPosition(position);
        int street_index = c.getColumnIndex(HistoryDbAdapter.KEY_STREET_NAME);
        int house_nr_index = c.getColumnIndex(HistoryDbAdapter.KEY_HOUSE_NUMBER);
        holder.text.setText(String.format("%s %s", c.getString(street_index), c.getString(house_nr_index)));
        return convertView;

    }

    static class ViewHolder {
        TextView text;
    }
}

Thats my first time. Maybe it can be writed more simple? maybe any speed suggestions?

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