Request multiple cells from SQLite database in android
我正在尝试从数据库中的两个不同的单元格调用数据,然后组合它们并在活动中打印出来。
我使用以下代码:
1 2 3 4 5 6 | public Cursor getGermanDescription(String id) { String[] args = { id }; return (getReadableDatabase() .rawQuery( "SELECT _id,Column1,Column2 FROM Databasing_Details WHERE _id=?", args)); |
通过上面的内容,我只得到第1列的内容,而不是第2列的内容。我正在将字符串ID传递给另一个活动。
我的光标适配器是:
@重写public void oncreate(bundle savedinstanceState){尝试{super.oncreate(保存的状态);setContentView(r.layout.main);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | //Get our passed variable from our intent's EXTRAS passedVar=getIntent().getStringExtra(ListViewTwo.ID_EXTRA); //this is our ListView element, obtained by id from our XML layout ListView myListView = (ListView)findViewById(R.id.list_view); String string = passedVar; int passedInt = Integer.parseInt(string); if (passedInt==1) { passedVar1 = true; } //create our database Helper dbDescriptionHelper=new DatabaseHelper(this); //a set of results from a database query ourCursor=dbDescriptionHelper.getGermanDescription(passedVar); //tell android to start managing the cursor, startManagingCursor(ourCursor); //create our adapter adapter=new SlangAdapter(ourCursor); //set the adapter!!! myListView.setAdapter(adapter); } catch (Exception e) { Log.e("ERROR","ERROR IN CODE:" + e.toString()); e.printStackTrace(); } return; } class SlangAdapter extends CursorAdapter { SlangAdapter(Cursor c) { super(ListViewFinal.this, c); } @Override public void bindView(View row, Context ctxt, Cursor c) { DescriptionHolder holder=(DescriptionHolder)row.getTag(); holder.populateFrom(c, dbDescriptionHelper); } @Override public View newView(Context ctxt, Cursor c, ViewGroup parent) { LayoutInflater inflater=getLayoutInflater(); View row=inflater.inflate(R.layout.main_row, parent, false); DescriptionHolder holder=new DescriptionHolder(row); row.setTag(holder); return(row); } } static class DescriptionHolder { private TextView name=null; DescriptionHolder(View row) { name=(TextView)row.findViewById(R.id.row); } void populateFrom(Cursor c, DatabaseHelper r) { name.setText(r.getName(c)); } } |
有人能指出我哪里出错了吗?
好的,我找到了答案。
我的语法不正确,我需要为光标编写的是:
1 2 3 4 5 6 7 8 9 10 | public Cursor getGermanDescription(String id) { String[] args = { id }; return (getReadableDatabase() .rawQuery( "SELECT _id, ObjectDescriptionGerman ||' '|| ObjectDescription FROM Databasing_Details WHERE _id=?", args)); } |
需要使用而不是","或"and"。我还在两个返回值之间插入了换行符,因此不需要在数据库中执行此操作。