搜索以编程方式创建的 Textview
我使用 for 循环以编程方式添加 TextViews,并使用 setId(Int int) 为该特定 Textview 设置唯一 id。 但现在我想根据该 id 搜索 textView。 我怎样才能搜索?
发生错误“应用程序意外停止...”这是我的代码。
public class Idtest extends Activity {
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll=new LinearLayout(this);
tv=new TextView(this);
tv.setText("Hello");
ll.addView(tv);
tv=new TextView(this);
tv=(TextView)ll.getChildAt(1);
setContentView(tv);
}
}
I'm adding TextViews programmatic using a for-loop and use setId(Int int) to set unique id for that perticular Textview.
But now I want to search textView on the basis of that id.
How can I search?
erorr occurd 'app stopped unexpectedly...' Here is my code.
public class Idtest extends Activity {
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll=new LinearLayout(this);
tv=new TextView(this);
tv.setText("Hello");
ll.addView(tv);
tv=new TextView(this);
tv=(TextView)ll.getChildAt(1);
setContentView(tv);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
tv
显然在末尾为null
(LinearLayout
只有一个子级,位于索引0
处),因此,您基本上是在调用setContentView(null)
,这会导致异常。我不清楚你想要做什么(你的代码非常混乱)。假设您尝试在
LinearLayout
中显示多个TextView
,这是我的建议:如果在以后的任何时候,您需要其中一个
TextView
>s, do:另外,请注意,从代码创建布局是邪恶的。如果你能避免它,请这样做。例如,如果
TextView
的数量是动态的,那么从代码创建它们是完全正常的(尽管您可以增加它们)。但是,不建议同时从代码创建LinearLayout
。您应该将其包含在 XML 中。如果您也可以在 XML 中包含TextView
,那就更好了。tv
is obviouslynull
at the end (theLinearLayout
only has one child, which is at index0
), so you're basically callingsetContentView(null)
which results in an exception. It's not clear for me what you're trying to do (your code is pretty messed up).Supposing you are trying to show multiple
TextView
s in aLinearLayout
, here's my suggestion:If, at any later point, you need one of the
TextView
s, do:Also, please note that creating layout from code is evil. If you can avoid it, please do. For example, if the number of
TextView
s is dynamic, then it's perfectly normal to create them from code (although you could inflate them). However, it's not advisable to also create theLinearLayout
from code. You should have that in an XML. If it's possible for you to also have theTextView
s in an XML, that would be even better.还有另一种选择...我们有
setTag()
和getTag()
方法。添加文本视图时为其指定一个标签,每当您想要进行搜索时,请使用 getTag()。There is another option too...we have
setTag()
andgetTag()
methods. While adding a textview give a tag for it and whenever u want to do a search use getTag().我想您正在将 TextView 控件添加到某些 ViewGroup (例如 LInearLayout)?
您可以使用 getChildCount() / getChildAt(index) 迭代 ViewGroup 子视图,并将子视图 ID 与您要搜索的视图 ID 进行比较。
I suppose you are adding your TextView controls to some ViewGroup (e.g. LInearLayout)?
You can iterate through the ViewGroup child views using getChildCount() / getChildAt(index) and compare the child view id with the one you're searching for.