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



Im new in realm db.我完成了在领域数据库中添加和获取数据。但是,我无法排序(升序和降序(。我的代码它在列表视图中显示项目。列表视图包含 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;
}

}

您的代码不会更改 db。您只是获得排序的项目,但不使用它们。

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

首先,在从 Realm 获取数据时,你不需要在 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();
  }

"result"列表应该在类级别声明,也应该传递给适配器的构造函数。

喜欢

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

相关内容

  • 没有找到相关文章

最新更新