如何将realmResult转换为数组并传递给另一个活动?

时间:2015-12-20 01:14:30

标签: java android realm

我的课程是列出一些学校,当我点击一所学校时,它会列出学生的姓名和身份证明。我正在使用Realm。

我能够列出学校并将学生的结果隐藏在realmResult中,但我无法将其传递给其他活动。

我想将realmResult转换为数组列表并将其传递给另一个带有数组适配器的活动。任何人都可以给我一些线索吗?....谢谢。

public class School extends RealmObject {

@Required
private String SchoolID;
private String SchoolName;
private RealmList<Student> Students;

getters / setters 
}


public class Student extends RealmObject{

@Required
private String StudentID;
private String StudentName;

}

主要活动     .....

RealmResults<School> schools = realm.where(School.class).findAll();
    final SchoolAdapter adapter = new SchoolAdapter(this, R.id.schools_list, schools, true);
ListView listView = (ListView) findViewById(R.id.schools_list);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


public void onItemClick(AdapterView<?> adapterView, View view,
                                int position, long id) {


    RealmList list  = adapter.getRealmResults().get(position).getStudents();

    RealmResults test = list.where().findAll();
            ....

学校适配器

public class SchoolAdapter extends RealmBaseAdapter<School> implements ListAdapter {

private static class ViewHolder{
    TextView school;
}

public SchoolAdapter(Context context, int resId, RealmResults<School> realmResults, boolean automaticUpdate) {
    super(context, realmResults, automaticUpdate);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.school = (TextView) convertView.findViewById(android.R.id.text1);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    School item = realmResults.get(position);
    viewHolder.school.setText(item.getSchoolName());
    return convertView;
}

public RealmResults<School> getRealmResults() {
    return realmResults;
}

}

2 个答案:

答案 0 :(得分:3)

感谢您的回复。我想我没有正确地初始化这个领域。

关于主要活动:

    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
    Realm.deleteRealm(realmConfiguration);
    Realm.setDefaultConfiguration(realmConfiguration);

    realm = Realm.getDefaultInstance();

关于学生活动,我应该在“创建时”

中有以下内容
    realm = Realm.getDefaultInstance();

答案 1 :(得分:2)

您可以轻松地将捆绑的SchoolID传递到下一个活动。

String someSchoolID = realmResults.get(position).getSchoolID();

Intent intent = new Intent(this, YourNewActivity.class);
Bundle bundle = new Bundle();
bundle.putExtra(A_KEY, someSchoolID);
intent.setArguments(bundle);
intent.startActivity();

活动中检索包和ID:

Bundle extras = getIntent().getExtras(); 
String someSchoolID;

if (extras != null) {
    someSchoolID = extras.getString(A_KEY);
} 

然后像这样获取实际的学校对象:

School school = realm.where(School.class).equalTo("SchoolID",      
someSchoolID).findFirst();