【问题标题】:How to list the Realm List in getView in an activity?如何在活动中列出 getView 中的领域列表?
【发布时间】:2015-12-15 21:29:43
【问题描述】:

我可以使用适配器在我的主要活动中打印学校名称,如下所示。

我的问题是,如果我点击学校名称并想在另一个学生活动中列出学生姓名,我该如何列出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
}

【问题讨论】:

    标签: java android realm


    【解决方案1】:

    1 - 你应该为你的学校列表视图实现一个 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 - 创建学生活动时,您应该在 OnCreate() 中按学校名称过滤学生,并在学生列表视图中显示他们。从意图中获取价值:

    // 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

    问:为什么需要在学生适配器中指定学生位置?

    【讨论】:

      【解决方案2】:
      you have to implement the onclick for school row and o click of that have to call another activity with putting school id as extra. 
      
      then on student you have to fetch students list on the basis of school id you send from first list. and the generate the list here.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2022-11-09
        • 1970-01-01
        相关资源
        最近更新 更多