2016-05-13 53 views
0

我第一次使用糖,似乎都工作得很好。我可以保存數據,並且似乎在我嘗試查找時正在獲取數據。我的問題是,我得到的對象,而不是我有存儲的價值,我不知道爲什麼,因爲我做的相同,我可以在官方文檔中看到我怎樣才能得到Android糖的價值,我只得到對象參考

這是我「M做來獲得數據:

Select name= Select.from(MyClass.class).where(Condition.prop("name").lt("Bob")); 

    String data = name.toString(); 

    Log.e("aaaaa", data.toString()); 

這是我在日誌中想趁自己:

[email protected] 
+0

好像你在'Select'對象中有一個結果數組或迭代器 - 這就是你所需要的。 – mihail

回答

0

隨着該Select語句,您要查詢的名稱MyClass的對象,那返回一個Select對象。

要從中獲取名稱,您應該從Select中獲取結果(這裏我使用.first())並將其放入MyClass對象中。然後你可以從中得到名字。

MyClass myClass = Select.from(MyClass.class) 
         .where(Condition.prop("name").lt("Bob")) 
         .first(); 

String name = myClass.getName(); 

提供您的MyClass的有一個方法getName()

+0

我試圖做到這一點,但當我嘗試構建項目時出現錯誤:(194,131)錯誤:不兼容類型 必需:MyClass 找到了:SugarRecord –

0

I'm doing the same that I can see in the official documentation

我不thnk你。

在文檔here,它顯示了這段代碼:

Select.from(TestRecord.class) 
.where(Condition.prop("test").eq("satya"), 
Condition.prop("prop").eq(2)) 
.list(); 

當你與你的代碼進行比較,你看,你錯過了呼叫list

如果您不撥打list,則where方法將返回Select對象。這就是你所得到的。

list將返回查詢找到的所有結果。所以正確的做法是:

List<MyClass> results = 
    Select.from(MyClass.class). 
    where(Condition.prop("name"). 
    lt("Bob")).list(); 

for (MyClass obj: results) { 
    Log.i("aaaa", obj.toString()); 
    // or you can write 
    // Log.i("aaaa", obj.name); 
} 

以上將打印所有結果。如果您只需要第一個結果,您可以致電results.get(0)