2013-02-07 18 views
0

如何添加获取该textview的引用,同时单击列表视图行中的按钮?“我有按钮和textview在一起列表视图行”,我想“更新textview值,同时单击按钮”在同一行?

** ---------------------------------

TextView的按钮

- -------------------------------- **

这是我列表视图的例子。 我已经使用查看持有人概念来动态操纵列表视图。 如果我点击按钮,textview的值想要改变。

我曾尝试更改texview值的值,同时单击按钮,但它更新每个按钮的最后一行textview值,我点击。 感谢和问候。

最后我得到的回答如下:

公共无效的onClick(视图v){// TODO自动生成方法存根

LinearLayout layout=(LinearLayout) v.getParent(); 
    TextView text=(TextView)layout.findViewById(R.id.qcountHD); 
    if (view.inc.getId() == v.getId()) { 
     int count = Integer.parseInt(text.getText().toString()); 
     text.setText("" + (++count)); 
    } else if (view.dec.getId() == v.getId()) { 
     int count = Integer.parseInt(text.getText().toString()); 
     if (!(count == 0)) { 
      text.setText("" + (--count)); 
     } 
    } 

} 

感谢支持.....

+0

发布您的代码。我需要为每行创建一个textviews列表。 – Raj

+2

发布一些代码对你有帮助 –

+0

看到这个链接。希望它会有所帮助。 :http://stackoverflow.com/questions/8385469/listview-with-textview-and-button-rowid-of-the-clicked-button – KDeogharkar

回答

1

最后,我得到了答案,我们可以使用父布局来获取任何子引用。 它指向当前的父母,它是孩子的​​,而你点击按钮。

public void onClick(View v) { 
     // TODO Auto-generated method stub 

    RelativeLayout layout=(RelativeLayout) v.getParent(); 
    TextView text=(TextView)layout.findViewById(R.id.qcountHD); 
    TextView product=(TextView)layout.findViewById(R.id.productHD); 
    TextView price=(TextView)layout.findViewById(R.id.priceHD); 


    Log.d("Current Product ",product.getText().toString()); 
    Log.d("Current Price ",price.getText().toString()); 
    Log.d("Current Quantity ",text.getText().toString()); 

    if (view.inc.getId() == v.getId()) { 
     int count = Integer.parseInt(text.getText().toString()); 
     text.setText("" + (++count)); 
    } else if (view.dec.getId() == v.getId()) { 
     int count = Integer.parseInt(text.getText().toString()); 
     if (!(count == 0)) { 
      text.setText("" + (--count)); 
     } 
    } 

} 

谢谢....

相关问题