Docs 菜单
Docs 主页
/ /
Atlas Device SDKs
/

显示集合 - Java SDK

在此页面上

  • 安装适配器
  • 示例模型
  • 在 ListView 中显示 collection
  • 在 RecyclerView 中显示 collection

Android 应用程序通常使用 RecyclerView 填充用户界面 ListView 组件。 Realm提供了 适配器 来显示域对象 集合 。这些集合实现了OrderedRealmCollections 接口。 RealmResults RealmList 是这些适配器的示例。使用这些适配器,用户界面组件会在您的应用更改Realm对象时进行更新。

将这些依赖项添加到应用程序级build.gradle文件中:

dependencies {
implementation 'io.realm:android-adapters:4.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}

Realm 在 JCenter 上托管这些适配器 工件存储库。要在 Android 应用程序中使用jcenter ,请将其添加到项目级build.gradle文件中:

buildscript {
repositories {
jcenter()
}
}
allprojects {
repositories {
jcenter()
}
}

提示

另请参阅:

源代码: 域/realm-android-adapters 在Github 上。

本页上的示例使用名为Item的 Realm 对象。 该类包含一个名为“name”的字符串和一个名为“id”的标识符编号:

import io.realm.RealmObject;
public class Item extends RealmObject {
int id;
String name;
public Item() {}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
import io.realm.RealmObject
open class Item(var id: Int = 0,
var name: String? = null): RealmObject()

ListView 中显示Realm对象 通过扩展 RealmBaseAdapter 。适配器使用ListAdapter接口。 实现方式与任何ListAdapter类似。 这为自动更新Realm对象提供了支持。

子类化RealmBaseAdapter以在ListView中显示Item对象:

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.mongodb.realm.examples.model.java.Item;
import io.realm.OrderedRealmCollection;
import io.realm.RealmBaseAdapter;
class ExampleListAdapter extends RealmBaseAdapter<Item> implements ListAdapter {
String TAG = "REALM_LIST_ADAPTER";
ExampleListAdapter(OrderedRealmCollection<Item> realmResults) {
super(realmResults);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
Log.i(TAG, "Creating view holder");
// create a top-level layout for our item views
LinearLayout layout = new LinearLayout(parent.getContext());
layout.setLayoutParams(
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
// create a text view to display item names
TextView titleView = new TextView(parent.getContext());
titleView.setLayoutParams(
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
// attach the text view to the item view layout
layout.addView(titleView);
convertView = layout;
viewHolder = new ViewHolder(titleView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// as long as we
if (adapterData != null) {
final Item item = adapterData.get(position);
viewHolder.title.setText(item.getName());
Log.i(TAG, "Populated view holder with data: " + item.getName());
} else {
Log.e(TAG, "No data in adapter! Failed to populate view holder.");
}
return convertView;
}
private static class ViewHolder {
TextView title;
public ViewHolder(TextView textView) {
title = textView;
}
}
}

要在活动中显示列表数据,请实例化ListView 。 然后,附加一个ExampleListAdapter

// instantiate a ListView programmatically
ListView listView = new ListView(activity.getApplicationContext());
listView.setLayoutParams(
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
// create an adapter with a RealmResults collection
// and attach it to the ListView
ExampleListAdapter adapter =
new ExampleListAdapter(
realm.where(Item.class).findAll());
listView.setAdapter(adapter);
ViewGroup.LayoutParams layoutParams =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
activity.addContentView(listView, layoutParams);
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ListAdapter
import android.widget.TextView
import com.mongodb.realm.examples.model.kotlin.Item
import io.realm.OrderedRealmCollection
import io.realm.RealmBaseAdapter
internal class ExampleListAdapter(realmResults: OrderedRealmCollection<Item?>?) :
RealmBaseAdapter<Item?>(realmResults), ListAdapter {
var TAG = "REALM_LIST_ADAPTER"
override fun getView(position: Int,
convertView: View?,
parent: ViewGroup): View {
var convertView = convertView
val viewHolder: ViewHolder
if (convertView == null) {
Log.i(TAG, "Creating view holder")
// create a top-level layout for our item views
val layout = LinearLayout(parent.context)
layout.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
// create a text view to display item names
val titleView = TextView(parent.context)
titleView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
// attach the text view to the item view layout
layout.addView(titleView)
convertView = layout
viewHolder = ViewHolder(titleView)
convertView.tag = viewHolder
} else {
viewHolder = convertView.tag as ViewHolder
}
// as long as we
if (adapterData != null) {
val item = adapterData!![position]!!
viewHolder.title.text = item.name
Log.i(TAG, "Populated view holder with data: ${item.name}")
} else {
Log.e(TAG, "No data in adapter! Failed to populate view holder.")
}
return convertView
}
private class ViewHolder(var title: TextView)
}

要在活动中显示列表数据,请实例化ListView 。 然后,附加一个ExampleListAdapter

// instantiate a ListView programmatically
val listView = ListView(activity!!.applicationContext)
listView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
// create an adapter with a RealmResults collection
// and attach it to the ListView
val adapter = ExampleListAdapter(realm.where(Item::class.java).findAll())
listView.adapter = adapter
val layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
activity!!.addContentView(listView, layoutParams)

RecyclerView 中显示Realm对象 通过扩展 RealmRecyclerViewAdapter 。适配器扩展了RecyclerView.Adapter 。 实现的工作方式与任何RecyclerView适配器类似。 这为自动更新Realm对象提供了支持。

子类化RealmRecyclerViewAdapter以在RecyclerView中显示Item对象:

import android.util.Log;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.mongodb.realm.examples.model.java.Item;
import io.realm.OrderedRealmCollection;
import io.realm.RealmRecyclerViewAdapter;
/*
* ExampleRecyclerViewAdapter: extends the Realm-provided
* RealmRecyclerViewAdapter to provide data
* for a RecyclerView to display
* Realm objects on screen to a user.
*/
class ExampleRecyclerViewAdapter
extends RealmRecyclerViewAdapter<Item,
ExampleRecyclerViewAdapter.ExampleViewHolder> {
String TAG = "REALM_RECYCLER_ADAPTER";
ExampleRecyclerViewAdapter(OrderedRealmCollection<Item> data) {
super(data, true);
Log.i(TAG, "Created RealmRecyclerViewAdapter for "
+ getData().size() + " items.");
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
Log.i(TAG, "Creating view holder");
TextView textView = new TextView(parent.getContext());
textView.setLayoutParams(
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return new ExampleViewHolder(textView);
}
@Override
public void onBindViewHolder(ExampleViewHolder holder,
int position) {
final Item obj = getItem(position);
Log.i(TAG, "Binding view holder: " + obj.getName());
holder.data = obj;
holder.title.setText(obj.getName());
}
@Override
public long getItemId(int index) {
return getItem(index).getId();
}
class ExampleViewHolder extends RecyclerView.ViewHolder {
TextView title;
public Item data;
ExampleViewHolder(TextView view) {
super(view);
title = view;
}
}
}

要在活动中显示列表数据,请实例化RecyclerView 。 然后,附加一个ExampleRecyclerViewAdapter

// instantiate a RecyclerView programmatically
RecyclerView recyclerView =
new RecyclerView(activity.getApplicationContext());
recyclerView.setLayoutManager(
new LinearLayoutManager(activity.getApplicationContext()));
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(
activity.getApplicationContext(),
DividerItemDecoration.VERTICAL));
// create an adapter with a RealmResults collection
// and attach it to the RecyclerView
ExampleRecyclerViewAdapter adapter =
new ExampleRecyclerViewAdapter(
realm.where(Item.class).findAll());
recyclerView.setAdapter(adapter);
ViewGroup.LayoutParams layoutParams =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
activity.addContentView(recyclerView, layoutParams);
import android.util.Log
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mongodb.realm.examples.model.kotlin.Item
import io.realm.OrderedRealmCollection
import io.realm.RealmRecyclerViewAdapter
/*
* ExampleRecyclerViewAdapter: extends the Realm-provided
* RealmRecyclerViewAdapter to provide data
* for a RecyclerView to display
* Realm objects on screen to a user.
*/
internal class ExampleRecyclerViewAdapter(data: OrderedRealmCollection<Item?>?) :
RealmRecyclerViewAdapter<Item?,
ExampleRecyclerViewAdapter.ExampleViewHolder?>(data, true) {
var TAG = "REALM_RECYCLER_ADAPTER"
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): ExampleViewHolder {
Log.i(TAG, "Creating view holder")
val textView = TextView(parent.context)
textView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
return ExampleViewHolder(textView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val obj = getItem(position)
Log.i(TAG, "Binding view holder: ${obj!!.name}")
holder.data = obj
holder.title.text = obj.name
}
override fun getItemId(index: Int): Long {
return getItem(index)!!.id.toLong()
}
internal inner class ExampleViewHolder(var title: TextView)
: RecyclerView.ViewHolder(title) {
var data: Item? = null
}
init {
Log.i(TAG,
"Created RealmRecyclerViewAdapter for ${getData()!!.size} items.")
}
}

要在活动中显示列表数据,请实例化RecyclerView 。 然后,附加一个ExampleRecyclerViewAdapter

// instantiate a RecyclerView programmatically
val recyclerView = RecyclerView(activity!!.applicationContext)
recyclerView.layoutManager =
LinearLayoutManager(activity!!.applicationContext)
recyclerView.setHasFixedSize(true)
recyclerView.addItemDecoration(
DividerItemDecoration(activity!!.applicationContext,
DividerItemDecoration.VERTICAL))
// create an adapter with a RealmResults collection
// and attach it to the RecyclerView
val adapter = ExampleRecyclerViewAdapter(realm.where(Item::class.java).findAll())
recyclerView.adapter = adapter
val layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
activity!!.addContentView(recyclerView, layoutParams)

后退

响应变更