开发者

how to check if a TextView exist

I created on loop 5 TextViews, set to them some text and added they to LinearLayout. After that I need to change text in them. I want to delete all of them and create new one on loop again. But before TextView deleting I must to be sure that its exists. How to do it?

for(int i=0; i<5; i++){
   TextView tv = new TextView(context);
   LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
   tv.setLayoutParams(开发者_开发问答params);
   tv.setId(i);
   tv.setText("some data");
   ll.addView(tv);
}

Thank you for help and your time!


If you want to delete all of them then just perform ll.removeAllViews().


You should keep a reference to each of the text views you create if you want to remove them. If I understand your question correctly, simply create an array of TextViews with 5 elements and run the loop to do what you want with them. To "delete" them you can call:

tv[i].setVisibility(View.GONE);

To change text and re-add them:

tv[i].setText("New Text");
tv[i].setVisibility(View.VISIBLE);


Better solution is to hold references to your TextViews in a list, so you can interact with them later on after you've created them. Otherwise you won't be able to see the objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜