ORMLite DISTINCT与Where Condition

时间:2017-12-07 13:32:26

标签: java android ormlite

我正在使用ORMLite开发一个Android项目,我有一个问题需要弄清楚。下面有一张图片,如何在 ORMLite 中使用DISTINCT sql查询? 我有一些参数给出条件,根据这个条件,我得到数字列表...

在这张图片中你会看到一些数字,其中一些数字相同,但我不想得到相同的数字,所以我如何在ORMLite中使用DISTINCT与where where条件。

感谢您的帮助

我的一些代码也是这样的:

  private void modalShowEbat() {
    dbHelper = (DBHelper) OpenHelperManager.getHelper(this, DBHelper.class);
    RuntimeExceptionDao<Segment, Integer> segmentDao = dbHelper.getSegmentExceptionDao();
    List<Segment> list = segmentDao.queryForEq(Constant.QUERY_SEGMENT, editTextSegment.getText().toString().trim());

    final CharSequence items[] = new CharSequence[list.size()];
    for (int i = 0; i < list.size(); i++) {
        items[i] = list.get(i).getColumnEbat();
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Segment seçiniz");
    builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // the user clicked on colors[which]
            editTextEbat.setText(items[which].toString());
            if (alertDialog != null && alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
        }
    });
    alertDialog = builder.create();
    alertDialog.show();
}

This is my image of result

2 个答案:

答案 0 :(得分:0)

使用此 -

segmentDao.queryBuilder().distinct().where().eq("name", "value").query();

答案 1 :(得分:0)

您好我用下面的代码解决了我的问题。也许你可以说这对此有很长的路要走。我编写了如下代码,因为我无法做其他方法(在ORMLite或其他ORMLite方法中使用.distinct()方法)

如果您有任何其他方法可以解决此问题,我正在等待您的建议。谢谢大家,快乐的编码...

private void modalShowEbat() {
    dbHelper = (DBHelper) OpenHelperManager.getHelper(this, DBHelper.class);
    RuntimeExceptionDao<Segment, Integer> segmentDao = dbHelper.getSegmentExceptionDao();
    List<Segment> list = segmentDao.queryForEq(Constant.QUERY_SEGMENT, editTextSegment.getText().toString().trim());

    GenericRawResults<String[]> rawResults = segmentDao.queryRaw("SELECT DISTINCT Ebat FROM Segment WHERE Segment = ? ", editTextSegment.getText().toString().trim());
    final CharSequence items[] = new CharSequence[list.size()];
    int i = 0;
    for (String[] resultColumns : rawResults) {
        String author = resultColumns[0];
        items[i] = author;
        i++;
    }
    int newLenght = 0;
    for (int j = 0; j < items.length; j++) {
        if (items[j] != null) {
            newLenght++;
        }
    }
    final CharSequence newItems[] = new CharSequence[newLenght];
    for (int j = 0; j < newLenght; j++) {
        if (items[j] != null) {
            newItems[j] = items[j];
        }
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose Segment");
    builder.setSingleChoiceItems(newItems, 0, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // the user clicked on colors[which]
            editTextEbat.setText(newItems[which].toString());
            if (alertDialog != null && alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
        }
    });
    alertDialog = builder.create();
    alertDialog.show();
}