2014-02-23 41 views
0

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

代码

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); 
+0

发布您的代码PLZ –

+0

其太长,但如果你真的想要它 – zxc

+0

如果你删除所有的死/注释掉的代码,它会更短 –

回答

1

不太清楚你遇到什么问题?如果它只是读你在循环中创建的所有TextViews,那么你就应该保持一个列表,并把它送上进行处理当您提交...

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

} 
+0

我在这里有一些问题,例如。我的textview-value在每次单击按钮时递增,我如何更新列表而不是插入列表的其他值 – zxc

+0

您的按钮似乎用于更新texview文本值。列表中的textviews是对同一个textview的引用(它们不是克隆,而是相同的textView),所以它们也会有更新的值。 – NameSpace

+0

@nNameSpace你是什么意思?即使我不把一个按钮onCLickEvent将更新列表它仍然会更新列表? – zxc

相关问题