如何对数据库中的项目进行分组并显示它们 - 安卓

2022-09-01 03:22:27

我在安卓应用程序中有收据和日志模型。收据有很多日志。

我按排序ID和等级查询group_by日志。

//recieve RecepitID and query to group logs
final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).groupBy("SortID, Grade").execute();

此分组工作正常。接下来是问题。我需要有这样的输出(红色圆圈中的部分):enter image description here但我得到的是这样的:

enter image description here

这是我如何做到这一点的代码的一部分。

public class LogsRecapitulation extends AppCompatActivity {

    private ListView mainListView;
    private BaseAdapter listAdapter;
    private TextView logsCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recapitulation_listview);
        mainListView = (ListView) findViewById(R.id.ListViewItem);

        //recieve RecepitID and query to group logs
        final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
        List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).groupBy("SortID, Grade").execute();

        TextView result = (TextView) findViewById(R.id.LogMassResult);
        double sum = 0.0;
        for (int i = 0; i < logsList.size(); i++) {
            sum += logsList.get(i).getM3();
        }
        result.setText(String.format("%.2f m3", sum));

        for (int i = 0; i < logsList.size(); i++) {
            if (logsList.get(i).receipt.priceType.equals("Na panju")) {
                TextView stumpPriceKN = (TextView) findViewById(R.id.sumPriceKN);
                double sumPricekn = 0.0;

                for (int j = 0; j < logsList.size(); j++) {
                    sumPricekn += logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3();
                }
                stumpPriceKN.setText(String.format("%.2f KN", sumPricekn));

            } else {
                TextView roadKN = (TextView) findViewById(R.id.sumPriceKN);
                double roadPrKn = 0.0;
                for (int j = 0; j < logsList.size(); j++) {
                    roadPrKn += logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3();
                }
                roadKN.setText(String.format("%.2f KN", roadPrKn));
            }
        }

        for (int i = 0; i < logsList.size(); i++) {
            if (logsList.get(i).receipt.priceCorrection > 0 && logsList.get(i).receipt.priceType.equals("Na panju")) {
                TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
                double correcSumKN = 0.0;
                for (int j = 0; j < logsList.size(); j++) {
                    correcSumKN += (logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3()) + ((logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3()) * logsList.get(j).receipt.priceCorrection / 100);
                }
                corecctionPriceKn.setText(String.format("%.2f KN", correcSumKN));
            } else if (logsList.get(i).receipt.priceCorrection > 0 && logsList.get(i).receipt.priceType.equals("Šumska cesta")) {
                TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
                double correcSumKN = 0.0;
                for (int j = 0; j < logsList.size(); j++) {
                    correcSumKN += (logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3()) + ((logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3()) * logsList.get(j).receipt.priceCorrection / 100);
                }
                corecctionPriceKn.setText(String.format("%.2f KN", correcSumKN));
            } else {
                TextView priceHolder = (TextView) findViewById(R.id.KorekcijaCijene);
                TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
                priceHolder.setText("");
                corecctionPriceKn.setText("");
            }
        }

        listAdapter = new RecapitulationArrayAdapter(logsList);
        mainListView.setAdapter(listAdapter);

        //display logs count
        logsCount = (TextView) findViewById(R.id.logsCount);
        logsCount.setText(String.valueOf(logsList.size()));
    }

    private class RecapitulationArrayAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<Logs> logsList;

        public RecapitulationArrayAdapter(List<Logs> logsList) {
            inflater = LayoutInflater.from(LogsRecapitulation.this);
            this.logsList = logsList;
        }

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

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

        @Override
        public long getItemId(int position) {
            return logsList.get(position).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.logs_recapitulation, parent, false);
            }
            Logs log = logsList.get(position);
            ((TextView) convertView.findViewById(R.id.rec_log_sort)).setText(log.sort_id);
            ((TextView) convertView.findViewById(R.id.rec_log_class)).setText(log.grade);
            ((TextView) convertView.findViewById(R.id.rec_log_count)).setText(String.valueOf(logsList.size()));
            ((TextView) convertView.findViewById(R.id.rec_logs_mass)).setText(String.format("%.2f m3", log.getM3()));

            if (log.receipt.priceType.equals("Na panju")) {
                ((TextView) convertView.findViewById(R.id.rec_log_price_default)).setText(String.valueOf(log.price.stumpPrice_kn));
            } else {
                ((TextView) convertView.findViewById(R.id.rec_log_price_default)).setText(String.valueOf(log.price.roadPrice_kn));
            }

            if (log.receipt.priceType.equals("Na panju")) {
                ((TextView) convertView.findViewById(R.id.rec_calculated_price)).setText(String.format("%.2f KN", log.price.stumpPrice_kn * log.getM3()));
            } else {
                ((TextView) convertView.findViewById(R.id.rec_calculated_price)).setText(String.format("%.2f KN", log.price.roadPrice_kn * log.getM3()));
            }

            return convertView;
        }
    }

}

我正在使用ActiveAndroid并在列表视图中显示它。

问题是显示这些分组的项目。我在BaseAdapter listView中显示它们,它只向我显示每个组一个项目,但它需要向我显示多个项目(因为它在每个组中有超过4个项目)。

任何人都可以给我建议,我应该做些什么来获得像第一张图像一样的输出?


答案 1

看起来您正在尝试获取类似于以下 SQL 的结果:

select sort, grade, count(grade), sum(mass), price from logs where receiptid = forwardedId group by sort, grade;

将显示计算为 的显示计数。相反,如果要让数据库对记录进行分组,则还需要从数据库获取计数。logList.size()

有几件事也可能有所帮助。1. 直接针对数据库验证 SQL 查询,以验证是否从预期的结果集开始。您可以使用 sqlite3 客户端进行测试,假设您使用的是 SQLite 数据库。2. 启用 ActiveAndroid 登录ActiveAndroid.initialize

此外,还可以仔细检查参数拼写,以确保引用预期的目的参数。

验证查询结果是否正确后,您可以专注于是否按预期在列表中呈现它。

最后,要警惕在UI线程上执行工作。


答案 2

用户“user650881”是正确的(由于repo<50,因此我无法投票)。

此外,为了获得总表的摘要,您可以执行以下 SQL 查询:

select /*sort, grade,*/ count(grade), sum(mass), price from logs where receiptid = forwardedId /*group by sort, grade*/;

如果您想按不同字段对项目进行分组,您也可以尝试寡妇函数,即:

select count(grade) over(partition by fieldName)

分组依据最终不是必需的


推荐