BaseAdapter从textView获取值并对其求和

时间:2016-03-05 11:10:34

标签: android baseadapter

我有一个基础适配器,我在其中放入了一些来自数据库的值。我在其中计算了一些值,并将其存储到textView。 因此,如果我的BaseAdapter在列表中有例如14项,我想得到所有这些计算值并求它们,并在listView之前显示它们。 我创建了一些代码,但我认为我做错了。 什么是最好的解决方案。 这是我的代码,一切都在发生。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_logs_listview);
        boolean sort = getIntent().getBooleanExtra("sort", false);
        mainListView = (ListView) findViewById(R.id.ListViewItem);
        final String place = (String) getIntent().getExtras().get("keyPlace");
        dbHandler = new LogsDBHandler(this);
        ArrayList<Logs> logsList = sort ? dbHandler.getAllLogsByPlace() : dbHandler.getAllLogs(place);
        listAdapter = new LogsArrayAdapter(logsList);
        mainListView.setAdapter(listAdapter);

        //HERE I TRIED TO GET ALL VALUES FORM TEXTVIEW AND SUM THEM
        for( int i=0; i<listAdapter.getCount(); i++) {
            View v = listAdapter.getView(i, null, null);
            TextView result = (TextView) v.findViewById(R.id.LogMassResult);
            double sum = result + Integer.parseInt(final_result.getText().toString());
            String final_sum = String.format("%.2f", sum);
            result.setText(final_sum);
        }
    }

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

    public LogsArrayAdapter(List<Logs> logsList) {
        inflater = LayoutInflater.from(DisplayLogs.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(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_display_logs, parent, false);
        }
        Logs log = logsList.get(position);
        ((TextView) convertView.findViewById(R.id.textDiameter)).setText(log.getDiameter());
        ((TextView) convertView.findViewById(R.id.textLength)).setText(log.getLength());
        double log_length, log_diameter, length, diameter, result;
        log_length = Integer.parseInt(log.getLength().toString());
        log_diameter = Integer.parseInt(log.getDiameter().toString());
        length = log_length / 100;
        diameter = log_diameter * log_diameter * 3.14159265;
        result = length * diameter / 40000;
        String final_result = String.format("%.2f", result);
        //FROM BELOW TEXTVIEW I WANT GET VALUES AND SUM THEM
        ((TextView) convertView.findViewById(R.id.textAmount)).setText(final_result);            
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:1)

为什么不移动计算&#34;结果&#34;的逻辑?到Logs类?

然后你可以像这样设置textview:((TextView)convertView.findViewById(R.id.textAmount)).setText(log.getResult());

总数:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_logs_listview);
    boolean sort = getIntent().getBooleanExtra("sort", false);
    mainListView = (ListView) findViewById(R.id.ListViewItem);
    final String place = (String) getIntent().getExtras().get("keyPlace");
    dbHandler = new LogsDBHandler(this);
    ArrayList<Logs> logsList = sort ? dbHandler.getAllLogsByPlace() : dbHandler.getAllLogs(place);

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

    listAdapter = new LogsArrayAdapter(logsList);
    mainListView.setAdapter(listAdapter);
}