gpt4 book ai didi

java - 如何获取android ListView 中某一列的总和?

转载 作者:行者123 更新时间:2023-12-01 18:35:28 26 4
gpt4 key购买 nike

我正在尝试获取 SQL 表中的数字总数,我首先在 android 和 sql 之间连接,但我不能将表格列中的数字相加并显示在 TextView 中,就像我想得到总数然后一列中的数字在 TextView 中显示它们,有人可以帮我完成这个任务吗?

这是我使用的代码 ListView

public class MyAppAdapter extends BaseAdapter         //has a class viewholder which holds
{
public class ViewHolder
{
TextView textDate;
TextView textTotal;
TextView textLastTotal;
TextView textCount;
TextView textTaxs;

}

public List<ClassListItems2> parkingList;

public Context context;
ArrayList<ClassListItems2> arraylist;

private MyAppAdapter(List<ClassListItems2> apps, Context context)
{
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ClassListItems2>();
arraylist.addAll(parkingList);
}

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

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

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{

View rowView = convertView;
ViewHolder viewHolder= null;
if (rowView == null)
{
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.listcontent2, parent, false);
viewHolder = new ViewHolder();
viewHolder.textDate = (TextView) rowView.findViewById(R.id.date);
viewHolder.textLastTotal = (TextView) rowView.findViewById(R.id.lasttotal);
viewHolder.textTaxs = (TextView) rowView.findViewById(R.id.date3);
viewHolder.textCount = (TextView) rowView.findViewById(R.id.InvoicescCount);
viewHolder.textTotal = (TextView) rowView.findViewById(R.id.total);
rowView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
// here setting up names and images
viewHolder.textDate.setText(parkingList.get(position).getDate()+"");
viewHolder.textTotal.setText(parkingList.get(position).getTotal());
viewHolder.textLastTotal.setText(parkingList.get(position).getLasttotal());
viewHolder.textTaxs.setText(parkingList.get(position).getTaxs());
viewHolder.textCount.setText(parkingList.get(position).getInvoicecount());





listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//What happens when you click on a place!



}
});

return rowView;
}

最佳答案

此适配器在其构造函数中包含一个集合(列表)。

要了解总数,请在处理该集合的 Activity/Fragment 中使用:

parkingList.size()

获取总和:

float total;

for (ClassListItems2 actualItem : parkingList) {
total += actualItem.getTotal()
}

所有这些逻辑都必须在 Activity/Fragment 中执行,而不能在 Adapter 中执行,因为 Adapter 仅具有表示/视觉逻辑。

关于java - 如何获取android ListView 中某一列的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60064564/

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