How to get the realm information from an second activity?

﹥>﹥吖頭↗ 提交于 2019-12-11 18:08:42

问题


I got the schoolID from the main activity, which is base on realm from a json file. Using the school ID I am trying to get the students name.

My question is how to get the students name and how to list the students from the student adapter. Thanks.

The following is the student activity:

@Override
public void onResume() {
    super.onResume();

    Bundle extras = getIntent().getExtras();

    if (extras != null) {

        String schoolName = extras.getString("schoolName");


        RealmResults<School> schools = realm.where(School.class).findAll();

        //RealmResults<School> school =  realmStudent.where(School.class).equalTo("SchoolName", schoolName).findAll();
        // Log.d(tag, String.valueOf(school));
    }
}

The following is the school class:

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

The following is the main activity:

@Override
public void onResume(){

    super.onResume();

    if(mAdapter == null) {
        List<School> schools = null;
        try {
            schools = loadSchools();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    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) {

         String schoolName  = adapter.getRealmResults().get(position).getSchoolName();

            startStudentList(schoolName);

        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    realm.close();
}

public void startStudentList(String schoolName){

    //String schoolName = school.getSchoolName();

    Intent schoolIntent = new Intent(this, StudentActivity.class);

    schoolIntent.putExtra("schoolName", schoolName);
   // Log.d(tag, String.valueOf(schoolName));

    startActivity(schoolIntent);


}

public List<School> loadSchools() throws IOException{

    loadJsonFromStream();

    return realm.allObjects(School.class);

}

private void loadJsonFromStream() throws IOException {

    InputStream stream = getAssets().open("school.json");

    realm.beginTransaction();
    try {
        realm.createAllFromJson(School.class, stream);
        realm.commitTransaction();
    } catch (IOException e) {

        realm.cancelTransaction();
    } finally {
        if (stream != null) {
            stream.close();


        }
    }

}

The following is the school adapter:

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;
}

}

回答1:


You Can use string array in XML file .



来源:https://stackoverflow.com/questions/34329224/how-to-get-the-realm-information-from-an-second-activity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!