【问题标题】:How to get single row from database and set the values of that row to textview's如何从数据库中获取单行并将该行的值设置为 textview
【发布时间】:2016-01-15 18:26:43
【问题描述】:

我是安卓应用开发的新手。我试图找到这个问题的解决方案,但最后我问这个问题没有任何用处。

1) 我正在应用程序中制作个人资料,但我想在 textviews 的帮助下只显示姓名和电子邮件。数据插入没有错误,但我不知道如何获取它并在 textview 上设置。这是我的数据库代码

public void User(String email,String name,SQLiteDatabase db){
    ContentValues contentValues = new ContentValues();
    contentValues.put(UserContract.NewUser.EMAIL,email);
    contentValues.put(UserContract.NewUser.NAME,name);
    db.insert(UserContract.NewUser.TABLE_NAME, null, contentValues);
    Log.e("Database Operations", "User Added...");
}

public Cursor getMyProfile() {
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT * FROM "+ UserContract.NewUser.TABLE_NAME;
    Cursor cursor = db.rawQuery(query,null);
    return cursor;
}

使用“用户”数据插入,但在“getMyProfile”中出现错误。这是我想将数据设置为 textview 的配置文件代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_profile);
    MyName = (TextView)findViewById(R.id.MyName);
    MyEmail = (TextView) findViewById(R.id.MyEmail);

    Cursor cursor = userHelper.getMyProfile();
    while (cursor.moveToFirst()){
        MyName.setText(cursor.getString(0));
        MyEmail.setText(cursor.getString(1));

    }

2) 谁能告诉我如何将图像保存在数据库中,意味着我想添加个人资料图片,但我不知道如何做任何建议。

提前致谢:)

【问题讨论】:

  • getMyProfile 遇到什么错误?
  • 这个while (cursor.moveToFirst()){ 可能是一个无限循环。您应该将 moveToFirst 放在 while 前面并在 while 中使用 moveToNext。或者因为您可能只有一条记录,所以不要使用 while 循环。只需使用 moveToFirst 并填充数据。
  • 当我切换到配置文件意图应用程序显示消息“应用程序没有响应”时,我也不确定代码是否正确或需要一些更改
  • 就像我说的:while (curosr.moveToFirst()) 在无限循环中。它永远不会退出。因为循环不断让光标中的第一个孩子。你的代码应该是Cursor cursor = userHelper.getMyProfile(); cursor.moveToFirst(); while (cursor.moveToNext()){ MyName.setText(cursor.getString(0)); MyEmail.setText(cursor.getString(1)); } 或者只是Cursor cursor = userHelper.getMyProfile(); cursor.moveToFirst(); MyName.setText(cursor.getString(0)); MyEmail.setText(cursor.getString(1));
  • 得到一个错误“java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MyProfile}: java.lang.NullPointerException"

标签: android performance sqlite android-activity


【解决方案1】:

1) 将您的功能修复为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_profile);
    MyName = (TextView)findViewById(R.id.MyName);
    MyEmail = (TextView) findViewById(R.id.MyEmail);

    Cursor cursor = userHelper.getMyProfile();
    cursor.moveToFirst();
    MyName.setText(cursor.getString(0));
    MyEmail.setText(cursor.getString(1));
}

在我的一个应用程序中,我使用for 循环来填充我的活动:

public void fillGrid(Cursor c){
        TableLayout tl = (TableLayout) findViewById(R.id.players_table);
        tl.removeAllViews();
        for(c.moveToFirst(); c.getPosition() < c.getCount(); c.moveToNext()){
            TableRow tr = new TableRow(this);
            TextView tv = new TextView(this);
            tv.setText(c.getString(1));
            tr.addView(tv);
            tl.addView(tr);
        }

        c.close();
    }

要选择数据,我执行以下操作:

    ...
        SQLiteDatabase db = tpdbHelper.getReadableDatabase();

        String[] projection = {
                Player.PlayerEntry._ID,
                Player.PlayerEntry.COLUMN_NAME_PLAYER_NAME
        };

        Cursor c = db.query(
                Player.PlayerEntry.TABLE_NAME,  //table name
                projection,                     //which columns
                null,                           //where clause columns
                null,                           //where clause values
                null,                           //group columns
                null,                           //filter by row groups
                null                            //order
        );

        fillGrid(c);
    ...

2) 您可以将图像的字节保存到 BLOB 列。我认为像 iunputStream 这样的东西可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多