2014-02-23 42 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

相關問題