如何从列表布局中的textview获取总和?

时间:2013-01-24 22:53:27

标签: android android-listview android-arrayadapter

我遇到编码如何计算用户在ListView中插入的所有数字的问题......

这是我的应用程序...我现在需要的是如何从列表视图中的行中获取总和并在另一个文本视图中显示它们:

enter image description here

这是我的主要活动:

public class MainActivity extends Activity {
    private Adapter adapter;
    private ArrayList<Ruka> fetch = new ArrayList<Ruka>();
    Button bt;
    EditText et;
    TextView tv;
    TextView mi;
    TextView vi;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Inflate the menu; this adds items to the action bar if it is present.
        bt = (Button) findViewById(R.id.button1);
        et = (EditText) findViewById(R.id.editText1);
        tv = (TextView) findViewById(R.id.textView1);
        mi = (TextView) findViewById(R.id.textViewMi);
        vi = (TextView) findViewById(R.id.textViewVi);
        lv = (ListView) findViewById(R.id.lista);

        // final String input = et.getText().toString();

        bt.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (et.getText().length() == 0) {
                    Toast.makeText(this, "test",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Float.valueOf(et.getText().toString()) >= 12) {
                    Toast.makeText(this,
                            "test2", Toast.LENGTH_SHORT)
                            .show();
                    return;
                }

                String input = et.getText().toString();
                if (null != input && input.length() > 0) {
                    String maxpunti = "11";
                    int a = Integer.parseInt(maxpunti);
                    int b = Integer.parseInt(input);
                    int c = a - b;
                    String input2 = String.valueOf(c);
                    Ruka one = new Ruka(input, input2);
                    fetch.add(one);
                    et.setText("");
                    adapter = new Adapter(MainActivity.this, R.id.lista, fetch);
                    lv.setAdapter(adapter);


                }

            }
        });

    }
}

这是我的适配器:

public class Adapter extends ArrayAdapter<Ruka>{
    private ArrayList<Ruka> entries;
    private Activity activity;

    public Adapter(Activity a, int textViewResourceId, ArrayList<Ruka> entries) {
        super(a, textViewResourceId, entries);
        this.entries = entries;
        this.activity = a;
    }

    public static class ViewHolder{
        public TextView item1;
        public TextView item2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi =
                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.item1 = (TextView) v.findViewById(R.id.textView1);
            holder.item2 = (TextView) v.findViewById(R.id.textView2);
            v.setTag(holder);
        }
        else
            holder=(ViewHolder)v.getTag();

        final Ruka custom = entries.get(position);
        if (custom != null) {
            holder.item1.setText(custom.getFirst());
            holder.item2.setText(custom.getSecond());
        }
        return v;
    }

}

更新

以下是Ruka.java

public class Ruka {
private String customBig;
private String customSmall;

public Ruka(String string, String string2) {
this.customBig = string;
this.customSmall = string2;
}
public String getFirst() { return customBig; }
public void setcustomBig(String getFirst) { this.customBig = getFirst; }

public String getSecond() { return customSmall; }
public void setcustomSmall(String getSecond) { this.customSmall = getSecond; }
}

如果有人能找到解决方案......请......任何人:)

1 个答案:

答案 0 :(得分:0)

我想说为你的适配器添加一个方法:

public int getSum(){
  int sum;
  for(Ruka ruka: entries){
    sum += ruka.getFirst();
  }
}

假设ruka.getFirst()是一个数值。