gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:45:48 30 4
gpt4 key购买 nike

我在 android 应用中有收据和日志模型。收据有很多日志。

我通过 sortID 和等级查询了 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 并在 ListView 中显示它。

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

谁能给我建议我应该怎么做才能获得第一张图片的输出?

最佳答案

您似乎正在尝试获得类似于以下 SQL 的结果:

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

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

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

中开启ActiveAndroid登录

您还可以仔细检查参数拼写以确保您引用了预期的 Intent 参数。

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

最后要小心在 UI 线程上工作。

关于java - 如何对数据库中的项目进行分组并显示它们 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37529619/

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