将SQLite记录显示为TextView(ANDROID)的一部分

时间:2013-06-15 18:28:22

标签: android sqlite

我的大多数Android问题都是从这个网站上回答的,但我有一个我无法找到的特殊问题。我想使用textview从SQLite数据库中检索记录的各个列。例如,

欢迎COLUMN1VALUE,根据我们的记录,您居住在COLUMN2VALUE,并且一直住在COLUMN3VALUE年。

有人能指出我的教程或给我一个答案吗?谢谢!

以下是代码:

      private void fillData(Uri uri) {
            String[] projection = { TodoTable.COLUMN_COREBELIEF,
                TodoTable.COLUMN_EXPERIENCE1,
                TodoTable.COLUMN_EXPERIENCE2,
                TodoTable.COLUMN_EXPERIENCE3,
                TodoTable.COLUMN_NEWBELIEF,};

        //    String query = "SELECT COLUMN_NEWBELIEF,COLUMN_COREBELIEF,COLUMN_EXPERIENCE1 FROM TodoTable";

            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);



    //          Cursor cursor = db.rawQuery(query, null);
                if (cursor != null) {
                      cursor.moveToFirst();
                String s = String.format("Welcome %s, from our records you are living in %s and have been living there for %s years.", cursor.getString(cursor.getColumnIndex("COLUMN_NEWBELIEF")),  cursor.getString(cursor.getColumnIndex("COLUMN_COREBELIEF")), cursor.getString(cursor.getColumnIndex("COLUMN_EXPERIENCE1")));
                textView1.setText(s);


              }



//            mCoreBelief.setText(cursor.getString(cursor
//                .getColumnIndexOrThrow(TodoTable.COLUMN_COREBELIEF)));
//            mExperience1.setText(cursor.getString(cursor
//                .getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE1)));
//            mExperience2.setText(cursor.getString(cursor
//                    .getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE2)));
//            mExperience3.setText(cursor.getString(cursor
//                    .getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE3)));
//            mNewBelief.setText(cursor.getString(cursor
//                    .getColumnIndexOrThrow(TodoTable.COLUMN_NEWBELIEF)));



              // Always close the cursor
              cursor.close();
            }

3 个答案:

答案 0 :(得分:1)

您可以通过以下代码轻松完成此操作:

 String query = "SELECT col1,col2,col3 FROM mytable";
 Cursor c = db.rawQuery(query, null);
 c.moveToFirst();
 String s = String.format("Welcome %s, from our records you are living in %s and have been living there for %s years.", c.getString(c.getColumnIndex("col1")),  c.getString(c.getColumnIndex("col2")), c.getString(c.getColumnIndex("col3")));
 txtView.setText(s);

答案 1 :(得分:0)

您的SQLite数据库是否已经存在,或者您是否需要有关如何在Android中管理SQLite数据库的整个生命周期的指导?

如果你的数据库已经存在,那么@hrehman的答案就足够了,否则Lar {@}}的Lar Vogel教程非常好。

答案 2 :(得分:0)

固定!!我修改了你的代码如下:

  private void fillData(Uri uri) {
      TextView textView1 = (TextView) findViewById(R.id.textView1);
        String[] projection = { TodoTable.COLUMN_COREBELIEF,
            TodoTable.COLUMN_EXPERIENCE1,
            TodoTable.COLUMN_EXPERIENCE2,
            TodoTable.COLUMN_EXPERIENCE3,
            TodoTable.COLUMN_NEWBELIEF,};
        Cursor cursor = getContentResolver().query(uri, projection, null, null,
            null);
        if (cursor != null) {
          cursor.moveToFirst();
          String s = String.format("Welcome %s, from our records you are living in %s and have been living there for %s years.", 
                  cursor.getString(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_NEWBELIEF)),  cursor.getString(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_COREBELIEF)), cursor.getString(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_EXPERIENCE1)));
          textView1.setText(s);
                      }

谢谢!