Android从分页查询列表

时间:2016-05-22 08:13:16

标签: android amazon-web-services arraylist pagination amazon-dynamodb

我已经能够查询AWS DynamoDb并获取结果。结果来自歌曲数据库和返回信息,如儿子的名字,音乐家,网址等。结果的数量按预期返回,所以我认为查询工作正常。这可能是非常基本的,但我不确定如何从列表中检索单个项目。

这是我的模特:

@DynamoDBTable(tableName = "Music")
public class SongDatabaseMappingAdapter {

    private String artist;
    private String songtitle;
    private String albumtitle;
    private String category;
    private int musicianrating;
    private int userrating;
    private String pictureurl;
    private String songurl;

    @DynamoDBHashKey(attributeName = "Artist")
    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    @DynamoDBRangeKey(attributeName = "SongTitle")
    public String getSongTitle() {
        return songtitle;
    }

    public void setSongTitle(String songtitle) {
        this.songtitle = songtitle;
    }


    @DynamoDBAttribute(attributeName = "AlbumTitle")
    public String getAlbumTitle() {
        return albumtitle;
    }

    public void setAlbumTitle(String albumtitle) {
        this.albumtitle = albumtitle;
    }


    @DynamoDBIndexHashKey(attributeName = "Category", globalSecondaryIndexName = "Category-UserRating-index")
    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }


    @DynamoDBAttribute(attributeName = "MusicianRating")
    public int getMusicianRating() {
        return musicianrating;
    }

    public void setMusicianRating(int musicianrating) {
        this.musicianrating = musicianrating;
    }


    @DynamoDBIndexRangeKey(attributeName = "UserRating", globalSecondaryIndexName = "Category-UserRating-index")
    public int getUserRating() {
        return userrating;
    }

    public void setUserRating(int userrating) {
        this.userrating = userrating;
    }


    @DynamoDBAttribute(attributeName = "PictureURL")
    public String getPictureURL() {
        return pictureurl;
    }

    public void setPictureURL(String pictureurl) {
        this.pictureurl = pictureurl;
    }


    @DynamoDBAttribute(attributeName = "SongURL")
    public String getSongURL() {
        return songurl;
    }

    public void setSongURL(String songurl) {
        this.songurl = songurl;
    }

}

这是我的疑问:

public class getSongsInCategory extends AsyncTask  {

        @Override
        protected Object doInBackground(Object[] params) {


            SongDatabaseMappingAdapter songs = new SongDatabaseMappingAdapter();
            songs.setCategory("Rap");

            String userRatingQueryString = "5";

            Condition rangeKeyCondition = new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ)
                    .withAttributeValueList(new AttributeValue().withN(userRatingQueryString));


            DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
                    .withHashKeyValues(songs)
                    .withIndexName("Category-UserRating-index")
                    .withRangeKeyCondition("UserRating", rangeKeyCondition)
                    .withConsistentRead(false);

            PaginatedQueryList<SongDatabaseMappingAdapter> result = mapper.query(SongDatabaseMappingAdapter.class, queryExpression);

这就是我查看结果的方式:

 for (int i = 0; i < result.size(); ++i) {

                SongDatabaseMappingAdapter obj = result.get(i);
                Log.i("Query Result", "Number"+i+obj.toString());
            }

这些是我得到的结果:

05-22 03:42:12.313 27594-27628/com.amazon.mysampleapp I/Query Result: Number0com.mysampleapp.demo.SongDatabaseMappingAdapter@52ae345c
05-22 03:42:12.313 27594-27628/com.amazon.mysampleapp I/Query Result: Number1com.mysampleapp.demo.SongDatabaseMappingAdapter@52ae520c
05-22 03:42:12.317 27594-27628/com.amazon.mysampleapp I/Query Result: Number2com.mysampleapp.demo.SongDatabaseMappingAdapter@52ae5334
05-22 03:42:12.317 27594-27628/com.amazon.mysampleapp I/Query Result: Number3com.mysampleapp.demo.SongDatabaseMappingAdapter@52ae545c 

所以我知道它至少部分有效但现在我想从歌曲查询中获取各个部分并将它们放入文本视图,图像视图媒体播放器等。

我可能错了,但我想我必须使用我创建的模型,但我不确定如何。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

SongDatabaseMappingAdapter实例有getter吗?因此,不要使用obj.toString()来执行obj.getArtist()等