Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make RealmList item as list view in adapter? #1952

Closed
kentcheung2000 opened this issue Dec 17, 2015 · 2 comments
Closed

How to make RealmList item as list view in adapter? #1952

kentcheung2000 opened this issue Dec 17, 2015 · 2 comments
Labels

Comments

@kentcheung2000
Copy link

I am still having a problem of how to list the realmList in my studentAdapter...I know how to list individual objects in viewholder in school adapter. I don't know how list the data from Students and put it in my student adapter which is similar to school adapter.

The StudentActivity is as followed:

@Override
public void onResume(){
    super.onResume();

    Bundle extras = getIntent().getExtras();

    if (extras != null)
    {
        String schoolName = extras.getString("schoolName");
        Log.d(tag, String.valueOf(schoolName));
    }

   I can get the school name but I don't know how to get the students name and list it as lisview in my student adapter.  Please help...

The MainActivity is as followed:

.......
@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);

        }
    });
}


public void startStudentList(String schoolName){

    //String schoolName = school.getSchoolName();

    Intent schoolIntent = new Intent(this, StudentActivity.class);

    schoolIntent.putExtra("schoolName", schoolName);


    startActivity(schoolIntent);
   }

The SchoolAdapter is as followed:

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

}
.....

The following is my School class:

public class School extends RealmObject {

@Required
private String SchoolID;
private String SchoolName;
private RealmList<Student> Students;


public RealmList<Student> getStudents() {
    return Students;
   }

public void setStudents(RealmList<Student> students) {
    Students = students;
   }
}

My Student class as followed:

   public class Student extends RealmObject{

@Required
private String StudentID;
private String StudentName;

getters/setters
}
@cmelchior
Copy link
Contributor

Our RealmBaseAdapter currently doesn't support RealmList (we have issue for it here: #1363), but you can e.g. convert a RealmList to a RealmResults by doing list.where().findAll(), or you can just use a normal ArrayAdapter (http://developer.android.com/reference/android/widget/ArrayAdapter.html) instead which should work as well.

@beeender
Copy link
Contributor

@kentcheung2000 Hope the above comment solved your issue. Feel free to reopen this if you still have related questions! Thanks a lot for you feedback!

@beeender beeender removed the Pending label Dec 25, 2015
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants