2011-08-03 36 views
1

您好我正在开发一个Android中,我从数据库中加载数据并在列表视图中显示它。 我的代码如下从SQLite显示两列在列表视图中相同的字符串android

public class rel extends ListActivity { 
@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.refill); 
    //Creating an object called mydbhelper 
    Dbms mydbhelper = new Dbms(this); 
    //Loading the database in writable format 
    SQLiteDatabase db=mydbhelper.getWritableDatabase(); 
    String[] id= new String[]{"_id","medicine","hs"}; 
    //Cursor 
    Cursor c = db.query("medicines",id,null,null,null, null,null); 
    startManagingCursor(c); 
    String[] from = new String[]{"medicine"}; 
    int[] to = new int[] { R.id.textlist1}; 
    // Now creating an array adapter and set it to display using my row 
    SimpleCursorAdapter notes =new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); 
    setListAdapter(notes); 



} 

什么我想要实现的是显示一个列表视图“医学-HS”中医药是从一列和HS是从数据库的另一列。例如 XYZ-HS1 ABC-HS2 作为列表视图内容

如何实现这一目标? 现在我能够得到的只有医药

请给你的那种建议

}

+0

这给了一个错误数据库中不存在这样的字段。更清楚地阅读问题并理解事情在内部如何工作,然后再试图展示你的聪明才智。 – 8A52

回答

1

我想说取代这个:

String[] from = new String[]{"medicine"}; 
    int[] to = new int[] { R.id.textlist1}; 

与此:

String[] from = new String[]{"medicine", "medicine-hs"}; 
    int[] to = new int[] { R.id.textlist1, R.id.textlist2}; 

你当然需要有一个R.id.textlist2可用在您的布局中使用。

编辑:恢复到以前的版本,它解决了问题。

+1

如果您需要进一步澄清,请告诉我。 – Jack

+0

嗨,这里的药物和hs来自数据库的不同列。我怎样才能让他们成为一个单一的字符串?问候钦尼克里希纳kothapalli – 8A52

+0

嗨杰克搞明白了。谢谢你的帮助。只需按照您的说明添加另一个文本框,并且需要重新定义XML布局以获取它。谢谢:-) – 8A52

1

你可以做的是执行rawQuery()方法了与SQL的DBHelper类结合,像这样的字段:

SELECT medicine + "-" + hs AS medicineHs FROM medicines ... 

因此字符串数组将

String[] from = new String[]{"medicineHs"}; 
+0

哇一个优雅的解决方案,但它没有选择'_id'列给出一个错误。其实我是SQL新手,所以可能会遇到问题:-) – 8A52

+1

哦,对。所以选择将是:SELECT _id,medicine +“ - ”+ hs AS药品从药品...... – adamcodes

相关问题