作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以按照以下代码使用适配器在我的主要 Activity 中打印学校名称。
我的问题是,如果我单击学校名称并想列出另一个学生 Activity 中的学生姓名,我该如何列出 RealmList<Student> Students
的学生姓名? ,以及如何在学生适配器中指定学生的位置?
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;
}
}
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;
//...getters/setters
}
最佳答案
1 - 你应该为你的学校 ListView 实现一个 OnItemClick 监听器:
listViewSchool.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
{
// Get the School Name from School Adapter
String schoolName = ((School) adapterView.getItemAtPosition(position)).[GetSchoolName()];
// Create intent to pass data and call another Activity.
Intent intent = new Intent(activity, [YourStudentActivity].class);
// Add data to intent
intent.putExtra("schoolName", schoolName);
// Call the Student Activity
startActivity(intent);
}
});
2 - 创建学生 Activity 时,您应该在 OnCreate() 中按学校名称过滤学生,并将他们显示在学生 ListView 中。要从 Intent 中获取值(value):
// OnCreate on Student Activity
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String schoolName = extras.getString("schoolName");
}
// Then filter the students by [schoolName] and then add the result to the student array used by the student adapter and then notify the student listview.
...
}
也许它可以帮助:Getting wrong result in Android application from PHP Server
Q:为什么需要在学生适配器中指定学生位置?
关于java - 如何在 Activity 中列出getView中的 Realm 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299748/
我是一名优秀的程序员,十分优秀!