如何遍历列表视图并获取所有编辑文本值的总和,并在文本视图中显示文本视图和值

时间:2016-07-14 09:39:44

标签: android list listview

这是我的适配器类。在我的列表中,我有搜索栏和编辑文本。搜索栏的值将显示在编辑文本中。我一直在尝试从所有编辑文本中获取值并总结它们并在文本视图中显示它。我想在编辑文本更改侦听器时执行此操作。所以总和值将在文本视图中同时更改。请帮我解决这个问题。谢谢你

   public class CustomListAdapter extends BaseAdapter {


    int value=0;

   ArrayList<RowItem>exp_name=new ArrayList<RowItem>();
    Context context;
    String TAG="myApp";
    StudyExpense study;



    LayoutInflater inflater=null;


    public CustomListAdapter(Context context,ArrayList<RowItem>exp_name)
    {

       this.context=context;
       this.exp_name= exp_name;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        return exp_name.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) {
        final View view;
        view=inflater.inflate(R.layout.seek_items,null);
        SeekBar seekBar=(SeekBar)view.findViewById(R.id.seekbarList);
        TextView txtname=(TextView)view.findViewById(R.id.seekName);


        final EditText txtprice=(EditText)view.findViewById(R.id.seekprice);
      txtprice.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {

          }

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {


          }

          @Override
          public void afterTextChanged(Editable s) {


          }
      });


        if(exp_name.get(position).getSeekValue()==0)
        {
            seekBar.setProgress(0);
            txtprice.setText("" + exp_name.get(position).getSeekValue());


        }
        else{
            seekBar.setProgress(exp_name.get(position).getSeekValue());
            txtprice.setText("" + exp_name.get(position).getSeekValue());


        }

        txtname.setText("" + exp_name.get(position).getTitle());

        seekBar.setTag(Integer.valueOf(position));
        seekBar.setId(position);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


                txtprice.setText("" + progress);

                exp_name.get(position).setSeekValue(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
               // int id;
               // id = seekBar.getId();
               // String title = exp_name.get(id).getTitle();

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        return view;




    }





}

主要活动课程(学习费用)

public class StudyExpense extends AppCompatActivity implements  {
    ListView listView;
    Toolbar toolbar;
    TextView total;
    ArrayList exp_name = new ArrayList();
    TextView sum;
    Button bt;
    CustomListAdapter adapter;

    String TAG = "myApp";

    public static final String[] titlelist = {"Study Expense", "Travelling Expense", "Household Expense", "Sports Expense", "Entertainment",
            "Food Expense", "Misc Expense"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study_expense);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        adapter = new CustomListAdapter(this, exp_name);
        total = (TextView) findViewById(R.id.txt_total);
        listView = (ListView) findViewById(R.id.listexp);
        sum = (TextView) findViewById(R.id.seekprice);
        bt=(Button)findViewById(R.id.button);

        listView.setAdapter(new CustomListAdapter(StudyExpense.this, exp_name));
        for (int i = 0; i < titlelist.length; i++) {
            RowItem item = new RowItem();

            item.setTitle(titlelist[i]);

            exp_name.add(item);


        }

1 个答案:

答案 0 :(得分:0)

StudyExpense

中创建列表大小的int []数组
public class StudyExpense extends AppCompatActivity implements  {
    ListView listView;
    public static ArrayList<Integer> valueslist;
    ...

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study_expense);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ....
        for (int i = 0; i < titlelist.length; i++) {
          RowItem item = new RowItem();
          item.setTitle(titlelist[i]);
          exp_name.add(item);
          }
         listView.setAdapter(new CustomListAdapter(StudyExpense.this, exp_name));
         valueslist =  new Arraylist<Integer>(exp_name.size());
        ...}

现在在CustomListAdapter方法中对getView()类进行一些修改。

   @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final View view;
        view=inflater.inflate(R.layout.seek_items,null);
        SeekBar seekBar=(SeekBar)view.findViewById(R.id.seekbarList);
        TextView txtname=(TextView)view.findViewById(R.id.seekName);


        final EditText txtprice=(EditText)view.findViewById(R.id.seekprice);
        txtprice.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {

          }

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {

            StudyExpense.valueslist.set(position, Integer.parseInt(s.toString()););
          }

          @Override
          public void afterTextChanged(Editable s) {


          }

  });

现在点击执行下面代码的任何按钮,找出小时数。

int sum = 0;
for (int i=0; i<StudyExpense.valueslist.size(); i++){
  sum += StudyExpense.valueslist.get(i);
}