获取动态生成的textView的值的方法

时间:2014-02-23 15:27:30

标签: android textview

当按下单击按钮时,我成功创建了一个动态TextView和一个Button.TextView的值发生了变化。
但问题是我在循环外面有一个最终的“提交按钮”,应该得到每个TextView的INDIVIDUAL值,我不能想办法如何做到这一点,有人请给我一个方法,谢谢!请好... ..

代码

Cursor citem= sdb.rawQuery("SELECT * FROM ITEM INNER JOIN CATEGORY ON item.categoryid = category.id where category.categoryname='"+fcat+"'", null);  
 ScrollView scrollView= new ScrollView(this);
        LinearLayout mainLayout= new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
Button border = new Button(this);
border.setId(Integer.parseInt(cuser.getString(cuser.getColumnIndex("id"))));;
    border.setText("ORDER");
    while (citem.moveToNext())
    {
        byte[] blob =citem.getBlob(citem.getColumnIndex("itemimage"));
        int id = Integer.parseInt(citem.getString(citem.getColumnIndex("id")));

            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setTag(id);
            int i;

            ImageView iv = new ImageView(this);
            iv.setId(id);
            iv.setImageBitmap(dh.getPhoto(blob));

            final TextView txtquantity = new TextView(this);
            txtquantity.setId(id);
            txtquantity.setText("0");
            txtquantity.setTextSize(20);

            final TextView txtprice = new TextView(this);
            txtprice.setId(id);
            txtprice.setText(citem.getString(citem.getColumnIndex("itemprice")));
            txtprice.setTextSize(30);

            ImageButton btn1 = new ImageButton(this);
            btn1.setId(id);
            final int id_ = btn1.getId();
            btn1.setImageResource(R.drawable.ic_launcher);
            btn1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    int i = 0;

                    i=Integer.parseInt((String) txtquantity.getText())+1;
                    txtquantity.setText(String.valueOf(i));
                    totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())+(Integer.parseInt(txtprice.getText().toString())*1)));
                }
            });                
            ImageButton btn2 = new ImageButton(this);
            btn2.setId(id);
            final int id2_ = btn2.getId();
            btn2.setImageResource(R.drawable.ic_launcher);
            btn2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {        
                    if(Integer.parseInt((String)txtquantity.getText())<=0)
                    {
                    }
                    else
                    {
                          int i=0; 
                          i= Integer.parseInt((String) txtquantity.getText())-1;
                            txtquantity.setText(String.valueOf(i));
                         totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())-(Integer.parseInt(txtprice.getText().toString())*1)));
                    }
                }
            });   
          linearLayout.addView(iv);
          linearLayout.addView(txtprice);
          linearLayout.addView(btn1);
          linearLayout.addView(txtquantity);
          linearLayout.addView(btn2);
          mainLayout.addView(linearLayout);



    }
     mainLayout.addView(totalprice);
     mainLayout.addView(border);
     scrollView.addView(mainLayout);
     setContentView(scrollView);

1 个答案:

答案 0 :(得分:1)

不太确定你遇到了什么问题?如果它只是读取你在循环中创建的所有TextView,那么你应该保留一个列表,并在你提交时将其发送出来进行处理......

List<TextView> tv_list = new ArrayList<TextView>();

while(...){

 //In loop..add your tv's
 TextView some_tv = new TextView()
 tv_list.add(some_tv);

 ...

}


//In the submit, send them off for processing...
private void process_tvs(List<TextView> tv_list){

    for(TextView tv:tv_list){
        //Assuming your tv's have numbers...
        int val = Integer.valueOf(tv.getText().toString());
        //do something....
    }

}