简体   繁体   English

如何从第二个活动中获取领域信息?

[英]How to get the realm information from an second activity?

I got the schoolID from the main activity, which is base on realm from a json file. 我从主要活动中获得了schoolID,该活动基于json文件中的领域。 Using the school ID I am trying to get the students name. 我正在使用学校ID来获取学生姓名。

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

}

您可以在XML文件中使用字符串数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android的第二个活动中显示信息 - How do I display information in my second activity in Android 如何从第二个活动中获取 ArrayList 并将其显示在主活动的 Recycler 视图中 - How to get ArrayList from second Activity and show it in a Recycler view in the Main Activity 从第二个活动中获取第一个活动中的列表计数值 - Get the list count value in first Activity from second Activity 如何从非活动到活动类接收信息 - how can receive information from non activity to activity class 如何将第一个Activity中的数据保存到第二个Activity中? - How to save the data in my ListView from the first Activity to the second Activity? Android-如何从活动2返回活动1的第二个片段? - Android - How to back to the second fragment on Activity 1 from Activity 2? 如何在第一个活动的第二个活动中设置图像? - How to set image in second Activity from first Activity? 如何从用户输入的编辑文本中获取信息并将其作为TextView移至另一个活动 - How to get information from an edit text that is entered by the user and move it to another activity as a TextView 我第一次从数据中获取信息时,Android如何去其他活动 - Android how to go to the other activity the first time I get information from data 从Realm获取Generic类型的Realm对象 - Get Generic type Realm Objects from Realm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM