【问题标题】:How to link data from another table to the current table [Android]如何将另一个表中的数据链接到当前表 [Android]
【发布时间】:2012-08-03 01:44:02
【问题描述】:

我需要从表中获取数据。然后我需要将现有数据与来自第二个表的另一个数据合并并将其显示为输出。

ImageSecondPage.java

package main.page;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class ImagesSecondpage extends Activity
{
     ImageAdapter imgDB = new ImageAdapter(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_secondpage);

        imgDB.open();
        Cursor c = imgDB.getAllImages();
        if(c.moveToFirst())
        {
            do
            {
            DisplayImages(c);

            }while(c.moveToNext());
        }
        imgDB.close();

    }

    public void DisplayImages(Cursor c)
    {
        int imageId = getResources().getIdentifier("main.page:drawable/pic1", null, null);
        ImageView imgView = (ImageView) findViewById(R.id.imageView1);
        imgView.setImageResource(imageId);
    }

}

目前,我只设法在活动中显示图像。但要求是我必须显示图像以及存储在另一个表中的人的姓名。现在我坚持要显示名称,因为我不知道如何从数据库的另一个表中获取名称。
任何帮助将不胜感激。非常感谢!

【问题讨论】:

    标签: android database image sqlite


    【解决方案1】:

    最简单的方法是拥有两个 Cursor 并使用 CursorJoiner 加入两者

    CursorJoiner joiner = new CursorJoiner(cursorA, keyColumnsofA, cursorB, keyColumnsofB);
     for (CursorJointer.Result joinerResult : joiner) {
         switch (joinerResult) {
             case LEFT:
                 // handle case where a row in cursorA is unique
                 break;
             case RIGHT:
                 // handle case where a row in cursorB is unique
                 break;
             case BOTH:
                 // handle case where a row with the same key is in both cursors
                 break;
         }
     }
    

    另一种选择是使用 SQLiteQueryBuilder 并使用 setTables 然后使用 query() 方法来获取您的光标。

    【讨论】:

    • 您好,感谢您回答我的问题。游标是我想链接在一起的表吗?我这样说对吗?
    【解决方案2】:

    据我所知,您必须使用原始查询:rawQuery(sql, selectionArgs); 或者您可以query(...) 获取每个表的cursor,然后将其相互比较。我认为第一种方式要短得多,但第二种方式更容易!

    编辑:第二种方式的示例代码,你可以访问这个人的教程vogella,第一种方式,你必须了解更多关于SQL声明:W3school .希望对您有所帮助!

    【讨论】:

    • 您好,感谢您回答这个问题,但您认为您可以提供一些示例代码吗?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多