我的应用程序中的领域数据库中排序不起作用

时间:2019-04-12 10:18:38

标签: android realm realm-mobile-platform realm-database

我是领域db中的新用户。我完成了在领域db中的添加和获取数据。但是,我无法排序(升序和降序)。我的代码在列表视图中显示项目。 Listview包含5个列表,每个列表包含4个字段(名称,年龄,技能和日期)。如果我对名称进行排序(升序),则需要在5个列表中升序。我的代码不起作用 我在这里发布代码,

 private void Ascending_order() {

    realm.beginTransaction();


  RealmResults<Employee> result = realm.where(Employee.class)
            .sort("name", Sort.ASCENDING).findAll();
    realm.copyFromRealm(result);
    realm.commitTransaction();
    employeedetailadapter.notifyDataSetChanged();
}

适配器类:

  public class EmployeeDetailAdapter extends BaseAdapter {

private ArrayList<Employee>employeeDetaillists = new ArrayList<>();
private Context c;
private LayoutInflater inflater;
private OnItemClick mCallback;
private SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");



public EmployeeDetailAdapter(Context c,ArrayList<Employee> employeeDetaillists, OnItemClick listener) {
    this.employeeDetaillists = employeeDetaillists;
this.c= c;
    inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mCallback = listener;

}

@Override
public int getCount() {
    return employeeDetaillists.size();
}

@Override
public Object getItem(int position) {
    return employeeDetaillists.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
Holder holder;

if (v==null){
    v= (View) inflater.inflate(R.layout.list_single_item,null);
    holder = new Holder();
    holder.tvPersonName = (TextView) v.findViewById(R.id.tvPersonName);
    holder.tvPersonAge = (TextView) v.findViewById(R.id.tvPersonAge);
    holder.tvPersonSkill = (TextView) v.findViewById(R.id.tvPersonSkill);
    holder.ivEditPesonDetail=(ImageView)v.findViewById(R.id.ivEditPesonDetail);
    holder.tvPersondate=(TextView)v.findViewById(R.id.tvPersondate);
    holder.ivDeletePerson=(ImageView)v.findViewById(R.id.ivDeletePerson);
    v.setTag(holder);
}else{
    holder = (Holder) v.getTag();
}
holder.tvPersonName.setText(employeeDetaillists.get(position).getName());
holder.tvPersonAge.setText(employeeDetaillists.get(position).getAge());
holder.tvPersonSkill.setText(employeeDetaillists.get(position).getSkill());
    String strDate = df.format(employeeDetaillists.get(position).getSdate());
holder.tvPersondate.setText(strDate);

holder.ivDeletePerson.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Delete(employeeDetaillists.get(position).getName(),position);

    }
});

return v;
}

private void Delete(String name, int position) {
    mCallback.onClickdelete(name, position);
}

public void updateData(RealmResults<Employee> result) {

}


class Holder {
    TextView tvPersonName, tvPersonAge, tvPersonSkill,tvPersondate;
    ImageView ivDeletePerson, ivEditPesonDetail;
}

}

2 个答案:

答案 0 :(得分:0)

您的代码不会更改数据库。您只是获得排序的项目,而不使用它们。

realm.copyFromRealm(result); // this line does nothing
realm.commitTransaction(); // this one too, because you change nothing
employeedetailadapter.notifyDataSetChanged(); // you data is the same, so this line also useless here

要查看已排序的数据,应在适配器中使用RealmResults。使用此方法,即使添加新项目后,您的列表也始终会排序。但请注意:您的适配器应扩展RealmRecyclerViewAdapter

您应该在创建适配器之前运行此代码,并在适配器内部使用result

RealmResults<Employee> result = realm.where(Employee.class)
        .sort("name", Sort.ASCENDING).findAll();

还可以尝试手动更新适配器的数据。

private void Ascending_order() {
    RealmResults<Employee> result = realm.where(Employee.class)
            .sort("name", Sort.ASCENDING).findAll();
    employeedetailadapter.updateData(result); // update data inside adapter before calling `notifyDataSetChanged`
    employeedetailadapter.notifyDataSetChanged();
 }

您需要自己创建updateData方法:

public void updateData(RealmResults<Employee> result) {
    employeeDetaillists = new ArrayList<Employee>(result);
}

答案 1 :(得分:-1)

首先,从Realm获取数据时,您无需在Transaction中写入数据。仅当您在领域中添加数据或更新任何领域对象时才需要Write Transaction。

关于您的问题,要从领域获取排序的数据,您可以这样做

RealmResults<Employee> result = realm.where(Employee.class).sort("name", Sort.ASCENDING).findAll();

现在,对获得的数据进行了排序,如果您仍然在ListView中看到错误的顺序,则适配器中可能存在一些问题。如果您共享适配器代码,那么我可以提供进一步的帮助:)

已更新:

适配器类

  public class EmployeeDetailAdapter extends BaseAdapter {

    private RealmResults<Employee> employeeDetaillists;
    private Context c;
    private LayoutInflater inflater;
    private OnItemClick mCallback;
    private SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");



    public EmployeeDetailAdapter(Context c,RealmResults<Employee> employeeDetaillists, OnItemClick listener) {
        this.employeeDetaillists = employeeDetaillists;
        this.c= c;
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mCallback = listener;

    }

    @Override
    public int getCount() {
        return employeeDetaillists.size();
    }

    @Override
    public Object getItem(int position) {
        return employeeDetaillists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        Holder holder;

        if (v==null){
            v= (View) inflater.inflate(R.layout.list_single_item,null);
            holder = new Holder();
            holder.tvPersonName = (TextView) v.findViewById(R.id.tvPersonName);
            holder.tvPersonAge = (TextView) v.findViewById(R.id.tvPersonAge);
            holder.tvPersonSkill = (TextView) v.findViewById(R.id.tvPersonSkill);
            holder.ivEditPesonDetail=(ImageView)v.findViewById(R.id.ivEditPesonDetail);
            holder.tvPersondate=(TextView)v.findViewById(R.id.tvPersondate);
            holder.ivDeletePerson=(ImageView)v.findViewById(R.id.ivDeletePerson);
            v.setTag(holder);
        }else{
            holder = (Holder) v.getTag();
        }
        holder.tvPersonName.setText(employeeDetaillists.get(position).getName());
        holder.tvPersonAge.setText(employeeDetaillists.get(position).getAge());
        holder.tvPersonSkill.setText(employeeDetaillists.get(position).getSkill());
        String strDate = df.format(employeeDetaillists.get(position).getSdate());
        holder.tvPersondate.setText(strDate);

        holder.ivDeletePerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Delete(employeeDetaillists.get(position).getName(),position);

            }
        });

        return v;
    }

    private void Delete(String name, int position) {
        mCallback.onClickdelete(name, position);
    }

    public void updateData(RealmResults<Employee> result) {

    }


    class Holder {
        TextView tvPersonName, tvPersonAge, tvPersonSkill,tvPersondate;
        ImageView ivDeletePerson, ivEditPesonDetail;
    }
}

在您的活动中,请更改以下功能

 private void Ascending_order() {
      result = realm.where(Employee.class)
           .sort("name", Sort.ASCENDING).findAll();
     employeedetailadapter.notifyDataSetChanged();
  }

“结果”列表应在类级别上声明,并且也应传递给适配器的构造函数。

    class Activity {
      RealmResults<Employee> result;
      EmployeeDetailAdapter employeedetailadapter;

      //// Other Code

        public onCreate(Bundle b) {
         result = realm.where(Employee.class).findAll();
               employeedetailadapter = new EmployeeDetailAdapter(this, result, listener);

          // Other code
         }
   }