如何分组数据库中的项目并显示它们 – Android
我有Android应用程序中的收据和日志模型。 收据有许多日志。
我通过sortID和grade来查询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();
这个分组工作正常。 接下来是问题。 我需要这样的输出(红色部分): 但是我明白这一点:
这是我如何做到这一点的代码的一部分。
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个以上项目)。
任何人都可以给我build议我应该怎么做才能得到像第一个图像的输出?
它看起来像你试图得到类似于以下SQL的结果:
select sort, grade, count(grade), sum(mass), price from logs where receiptid = forwardedId group by sort, grade;
您显示的计算结果显示为logList.size()
。 相反,如果你打算让数据库组成为logging,那么你也需要从数据库中获取计数。
一些事情也可能有帮助。 1.直接对数据库validationSQL查询,以validation您是从预期的结果集开始的。 您可以使用sqlite3客户端来testing,假设您正在使用SQLite数据库。 2.在ActiveAndroid.initialize
启用ActiveAndroid日志logging
您也可以仔细检查参数拼写,以确保您正在引用预期的意图参数。
一旦你确认查询结果是正确的,你可以关注你是否在你想要的列表中。
最后要小心在UI线程上工作。
用户'user650881'是正确的(我不能投票自回购<50)。
此外,要实现总表的摘要,您可以执行以下SQL查询:
select /*sort, grade,*/ count(grade), sum(mass), price from logs where receiptid = forwardedId /*group by sort, grade*/;
如果你想有不同的领域分组的项目,你也可以尝试寡妇function,即:
select count(grade) over(partition by fieldName)
最后没有必要组合