表格字段:
_id,street,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"
感谢
答案 0 :(得分:3)
有两种方法:
Concat查询中的两个列名称:KEY_STREET_NAME +“||”+ KEY_HOUSE_NUMBER。我会使用rawQuery。
使用自定义适配器。
答案 1 :(得分:0)
有效:
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;
}
}
这是我的第一次。也许它可以写得更简单?也许任何速度建议?