将Realm变量从其他类传递到适配器中的getView

时间:2016-02-01 20:37:37

标签: java android realm

在我的主要活动中,我喜欢将另一个类的结果值传递给我的适配器中的getview。

我可以将变量radioPosition传递给我的适配器,但不能在我的getView()中使用它;

我的问题是如何将我的Setting类中的realm变量传递给我的School适配器中的getview()

主要活动

public class MainActivity extends AppCompatActivity {

    ....
    Setting setting = realm.where(Setting.class).findFirst();
    //radioPosition is from Setting Class
    Integer radioPosition = setting.getPosition();
    RealmResults<School> schools = realm.where(School.class).findAll();
    //schools is from School Class

    final SchoolAdapter adapter = new SchoolAdapter(this, R.id.schools_list, schools, Integer radioPosition, true);
    ....
}

学校适配器:

public class SchoolAdapter extends RealmBaseAdapter<School> implements ListAdapter {

    ....
    public SchoolAdapter(Context context, int resId, RealmResults<School> realmResults,  Integer radioPosition, 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();
        }

    .....

1 个答案:

答案 0 :(得分:1)

  private int mRadioPosition;

  public SchoolAdapter(Context context, int resId, RealmResults<School> realmResults,  Integer radioPosition, boolean automaticUpdate) {

        super(context, realmResults,  automaticUpdate);
        mRadioPosition = radioPosition;
    }

在getView方法中使用mRadioPosition。如果我误解了你的问题,请告诉我。