gpt4 book ai didi

android - Realm 搜索的复杂排序,多个 RealmResults 的联合

转载 作者:IT王子 更新时间:2023-10-29 06:32:12 33 4
gpt4 key购买 nike

我在我的开源 Linux 命令库项目中用 realm 替换了 sqlite。到目前为止一切顺利,但现在我遇到了问题。

我正在使用 RealmBaseAdapter 在带有搜索界面的 ListView 中显示所有命令。对于搜索,下面狙击的 Realm 会按如下顺序排列结果:

查询:测试

结果:

  • l2测试
  • rc测试
  • 测试
  • 测试参数

    RealmResults<Command> commands = mRealm.where(Command.class).contains("name", query).findAll();
    mAdapter.updateRealmResults(commands);

使用旧的 sqlite 逻辑,顺序是这样的:

结果:

  • 测试
  • 测试参数
  • l2测试
  • rc测试

return getReadableDatabase().rawQuery("Select * from "+ CommandsDBTableModel.TABLE_COMMANDS + "WHERE "+ CommandsDBTableModel.COL_NAME + "LIKE '%"+ query + "%' "+ "ORDER BY "+ CommandsDBTableModel .COL_NAME + "= '"+ query + "' DESC,"+ CommandsDBTableModel.COL_NAME + "LIKE '"+ query + "%' DESC", null);

是不是也可以用realm来实现呢?这是项目的链接 https://github.com/SimonSchubert/LinuxCommandBibliotheca

最佳答案

谢谢你们,你们帮了我很多,解决了这个问题。 @Mateusz Herych @EpicPandaForce

这是自定义适配器:

public abstract class RealmMultiAdapter<T extends RealmObject> extends BaseAdapter {

private final RealmChangeListener<T> realmChangeListener = new RealmChangeListener<T>() {
@Override
public void onChange(RealmResults<T> t) {
notifyDataSetChanged();
}
};

protected LayoutInflater inflater;
protected List<RealmResults<T>> realmResults;
protected Context context;

public RealmMultiAdapter(Context context, List<RealmResults<T>> realmResults, boolean automaticUpdate) {
if (context == null) {
throw new IllegalArgumentException("Context cannot be null");
}
this.context = context;
this.realmResults = realmResults;
this.inflater = LayoutInflater.from(context);
for(RealmResults<T> results : realmResults) {
results.addChangeListener(realmChangeListener);
}
}

/**
* Returns how many items are in the data set.
*
* @return count of items.
*/
@Override
public int getCount() {
if (realmResults == null) {
return 0;
}
int count = 0;
for(RealmResults<T> realmResult : realmResults) {
count += realmResult.size();
}
return count;
}

/**
* Returns the item associated with the specified position.
*
* @param i index of item whose data we want.
* @return the item at the specified position.
*/
@Override
public T getItem(int i) {
if (realmResults == null || realmResults.size()==0) {
return null;
}
int count = 0;
for(RealmResults<T> realmResult : realmResults) {
if(i<realmResult.size()+count) {
return realmResult.get(i-count);
}
count += realmResult.size();
}
return null;
}

/**
* Returns the current ID for an item. Note that item IDs are not stable so you cannot rely on the item ID being the
* same after {@link #notifyDataSetChanged()} or {@link #updateRealmResults(List<RealmResults<T>>)} has been called.
*
* @param i index of item in the adapter.
* @return current item ID.
*/
@Override
public long getItemId(int i) {
// TODO: find better solution once we have unique IDs
return i;
}

/**
* Updates the RealmResults associated to the Adapter. Useful when the query has been changed.
* If the query does not change you might consider using the automaticUpdate feature.
*
* @param queryResults the new RealmResults coming from the new query.
*/
public void updateRealmResults(List<RealmResults<T>> queryResults) {
for(RealmResults<T> results : realmResults) {
if(results.isValid()) {
results.removeChangeListener(realmChangeListener);
}
}
this.realmResults = queryResults;
for(RealmResults<T> results : realmResults) {
results.addChangeListener(realmChangeListener);
}
notifyDataSetChanged();
}
}

基本上,我用 RealmResults 列表替换了单个 RealmResult,并修改了 getItem() 和 getCount() 方法。

    // before
protected RealmResults<T> realmResults;
// after
protected List<RealmResults<T>> realmResults;

这就是我更新搜索的方式

    List<RealmResults<Command>> results = new ArrayList<>();
results.add(mRealm.where(Command.class).equalTo("name", query).findAll());
results.add(mRealm.where(Command.class).beginsWith("name", query).notEqualTo("name", query).findAll());
results.add(mRealm.where(Command.class).contains("name", query).not().beginsWith("name", query).notEqualTo("name", query).findAll());

mAdapter.updateRealmResults(results);

关于android - Realm 搜索的复杂排序,多个 RealmResults 的联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34975080/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com