在textview中获取db字符串

时间:2016-10-23 10:31:48

标签: android database textview

我将数据库中的数据读入String。现在我想把这个String放到textview中以在列表中显示我的数据。我执行read方法并希望显示返回字符串mit数据。问题是:Android工作室无法解析"符号"又名dbString。

Databasetostring:

public String databaseToString(){
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    //Every Column and row
    String query = "SELECT * FROM " + TABLE_TODO + " WHERE 1";

    //Cursor points to a location in your results
    //First row point here, second row point here

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();

    while(!c.isAfterLast()){
        //Extracts first name and adds to string
        if(c.getString(c.getColumnIndex("firstName"))!=null){
            dbString += c.getString(c.getColumnIndex("firstName"));
            c.moveToNext();
        /*
         * Displaying all other columns
         */
        }
    }
    db.close();
    return dbString;

MainActivity:

Button refresh_list;
ToDoDB showDb;
TextView datalist;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    datalist = (TextView)findViewById(R.id.showallData);
    refresh_list = (Button)findViewById(R.id. refresh_list);
    refresh_list.setOnClickListener(
            new View.OnClickListener(){
                @Override
                public void onClick(View view) {

                    datalist.setText(dbString);
                }
            });

}

0 个答案:

没有答案