在Android上,我怎么能建立从境界列表的领域适配器? [英] On Android, how can I build a Realm Adapter from a Realm List?
问题描述
我在Android上使用境界写的食谱应用程序。我有型成分在每个配方对象RealmList。对象创建code是工作的罚款。
I'm writing a recipe app on Android using Realm. I have a RealmList of type Ingredient in each Recipe object. The object creation code is working fine.
现在我正在写的code为显示单个配方的片段。我能创造一个领域适配器的所有配方标题列表,因为我使用内置的查询,像这样的列表:
Now I'm writing the code for the Fragment that displays a single recipe. I was able to create a Realm Adapter for the all recipe titles list, since I built that list using a query like so:
public class RecipeTitleAdapter extends RealmBaseAdapter<RecipeTitle> implements ListAdapter {
public RecipeTitleAdapter(Context context, int resId,
RealmResults<RecipeTitle> realmResults,
boolean automaticUpdate) {
...
recipeTitles = RecipeTitle.returnAllRecipeTitles(realm);
final RecipeTitleAdapter adapter = new RecipeTitleAdapter(RecipeParserApplication.appContext, R.id.recipe_list_view, recipeTitles, true);
但现在,我在看的成分为单一的配方,我有成分的RealmList而不是RealmResults对象。我的成分适配器类具有相同类型构造为配方标题适配器,所以我想知道如何(或者即使)我可以把它的工作开始了RealmList。
But now that I'm looking at the ingredients for a single recipe, I have a RealmList of Ingredients and not a RealmResults object. My ingredient adapter class has the same type of constructor as the recipe titles adapter, so I want to know how (or even if) I can make it work starting with a RealmList.
public class IngredientAdapter extends RealmBaseAdapter<Ingredient> implements ListAdapter {
private static class ViewHolder {
TextView quantity;
TextView unitOfMeasure;
TextView ingredientItemName;
TextView processingInstructions;
}
public IngredientAdapter(Context context, int resId,
RealmResults<Ingredient> realmResults,
boolean automaticUpdate) {
....
final IngredientAdapter adapter = new IngredientAdapter(RecipeParserApplication.appContext, R.id.ingredientListView, recipe.getIngredients(), true);
public RealmList<Ingredient> getIngredients() {
return ingredients;
}
由于recipe.getIngredients返回RealmList,其中IngredientAdapter分配收益编译错误行:
Since recipe.getIngredients returns a RealmList, the line where the IngredientAdapter is assigned returns a compile error:
错误:(63,43)误差:构造IngredientAdapter类IngredientAdapter不能被应用到给定的类型;
要求:方面,诚信,RealmResults,布尔
发现:上下文,INT,RealmList,布尔
原因:实际参数RealmList无法通过方法调用转换转换为RealmResults
Error:(63, 43) error: constructor IngredientAdapter in class IngredientAdapter cannot be applied to given types; required: Context,int,RealmResults,boolean found: Context,int,RealmList,boolean reason: actual argument RealmList cannot be converted to RealmResults by method invocation conversion
推荐答案
一个RealmList如同一个正常的数组,因此,如果您不能进行查询匹配要显示,你可以使用任何正常的适配器像什么例如一个ArrayAdapter。使用RealmBaseAdapter的唯一优点是它autorefreshes,但是这是相当容易完成自己:
A RealmList behaves like an normal array, so if you cannot make a query that matches what you want to display, you can just use any of the normal adapters like e.g. a ArrayAdapter. The only advantage of using a RealmBaseAdapter is that it autorefreshes, but that is fairly easy to accomplish yourself:
// Pseudo code
ArrayAdapter adapter;
RealmChangeListener listener = new RealmChangeListener() {
public void onChange() {
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
}
protected void onCreate(Bundle savedInstanceState) {
// ...
realm.addChangeListener(listener);
RealmList data = getRealmListData();
adapter = new ArrayAdapter(data);
listView.setAdapter(adapter);
}
这篇关于在Android上,我怎么能建立从境界列表的领域适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!