gpt4 book ai didi

java - 如何在 Activity 中列出getView中的 Realm 列表?

转载 作者:行者123 更新时间:2023-11-29 20:14:07 24 4
gpt4 key购买 nike

我可以按照以下代码使用适配器在我的主要 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com