从数据库获取textview id并使用它

时间:2017-07-29 17:19:27

标签: java android database sqlite

我正在开发一个android项目。在我的sqlite数据库中,我得到了一些与这些问题相关的问题,答案和textview id。在我的xml中,我有" textA&#34 ;," textB"," textBLABLA"。我也在我的数据库中有这些ID。

要从数据库获取textview id,我使用此sql代码。

private void getText(int id) {
Cursor getTextView = myDatabase.rawQuery("SELECT textView FROM questions WHERE id = ?", new String[] {String.valueOf(id)});
int textIndex = getTextView.getColumnIndex("textView");

我可以获得该值。但是我如何使用此值来编辑该textview?比如改变它的背景颜色;

questionTextView.setBackgroundColor(Color.BLACK);

1 个答案:

答案 0 :(得分:0)

你刚刚获取了列索引,而不是列中的值。

private void getText(int id) {
Cursor getTextView = myDatabase.rawQuery("SELECT textView FROM questions WHERE id = ?", new String[] {String.valueOf(id)});
int textIndex = getTextView.getColumnIndex("editText"); // this gives you the column index you requested

// AD THIS AFTER ABOVE CODE
if(getTextView.moveToNext()){
    int textViewId = getTextView.getInt(textIndex); // this gives you the value from the column index you provide and and the row you on which the current cursor is
    TextView questionTextView = (TextView) findViewById(textViewId); 
    /* UPDATE */
    questionTextView.setBackgroundColor(Color.Black);
}

您需要从获取的列索引中获取值。

相关问题