获取共享父与当前视图中的其他子 [英] Get other child that shares parent with the current view

查看:209
本文介绍了获取共享父与当前视图中的其他子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个表,其中每一行包含文本和一个按钮。现在,当该按钮为pressed我想打电话给使用按钮旁边的文本价值的事件。我如何可以访问的TextView的内容?我可以得到 ViewParent 我的按钮,这应该是该行的,但有没有方法来访问该意见的孩子。

 私人OnClickListener updateButtonListener =新OnClickListener(){
     公共无效的onClick(视图v){
         ViewParent父= v.getParent();
         //这里我想获得该ViewParent的第一个孩子
      }
};
 

解决方案

如果你能得到一个 ViewParent ,你可以将其转换为 ViewGroup中并获得查看需要。您的code将是这样的:

 的TextView TextView的= NULL;
ViewGroup中排=(ViewGroup中)v.getParent();
为(中间体itemPos = 0; itemPos&其中; row.getChildCount(); itemPos ++){
    查看查看= row.getChildAt(itemPos);
    如果(查看的instanceof的TextView){
         TextView的=(TextView的)观点; //找到了!
         打破;
    }
}
 

就是这样,假设你只有一个的TextView 在你行。

I'm creating a table in which each row contains text as well as a button. Now when that button is pressed I want to call an event that uses the value of the text next to the button. How can I access the content of that TextView? I can get the ViewParent of my button, which should be the row, but there's no method to access that views children.

private OnClickListener updateButtonListener = new OnClickListener(){
     public void onClick(View v) {
         ViewParent parent = v.getParent();
         //Here I would like to get the first child of that ViewParent
      }
};

解决方案

If you can get a ViewParent, you can then cast it to a ViewGroup and get the View you need. Your code will look like this:

TextView textView = null;
ViewGroup row = (ViewGroup) v.getParent();
for (int itemPos = 0; itemPos < row.getChildCount(); itemPos++) {
    View view = row.getChildAt(itemPos);
    if (view instanceof TextView) {
         textView = (TextView) view; //Found it!
         break;
    }
}

That's it, assuming you have only one TextView in your row.

这篇关于获取共享父与当前视图中的其他子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆