1

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

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

在此处输入图像描述

这是我的主要活动:

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; }
}

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

4

1 回答 1

0

我会说向您的适配器添加一个方法来执行此操作:

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

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

于 2013-01-24T23:08:15.830 回答