android


Is there a way to distinguish between TextViews or any other Views having same ID's inflated multiple times from a single xml file


I hope I get an answer to my question this time, I already posted a question on this but I guess people overlooked it since it was not put properly, here is a link to my previous question How do I know the ID's of views if I am inflating a view multiple times in the same parent on button click?
I am inflating child.xml which contains a TableLayout with 3 LinearLayouts which again contain TextView's in my main Activity on button click event, the user can add as many views as he wants.
But I am confused as of how to collect the data from those textviews or setOnClickListeners to them since different views share the TextView's which same Id.
People suggested me to use findViewById on the inflated view by appending it with appropriate type and store the reference in a List, but I am able to set an OnClickListener only for the first view that is inflated.
I wanted to know if it is even possible to achieve what I am doing? In an other post I read that findViewById finds the first possible instance of the view it can find. Is that a problem in my case.
Please have a look at my code snippet
TextView[] tv=new TextView[l.size()];
for(int i=0;i<tv.length;i++){
tv[i]=(TextView)l.get(i).findViewById(R.id.mdsnew_type_sp);
tv[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID); }
});
//I have also tried this
public void getData(){
for(int i=0;i<l.size();i++){
tv1=(TextView)l.get(i).findViewById(R.id.mdsnew_type_sp);
}
But I am able to setOnClick Listener only for the first TextView and the rest are all ignored.
Here is another link that explains how I inflate my activity and a similar scenario but getting the information is not explained there Inflating a view multiple times in the same parent when a button is clicked
Any help will be rewarded, thanks in advance.
Since these TextViews have been added to a parent ViewGroup (in your case, a LinearLayout), you can traverse the parent's list of child views, and add an OnClickListener to each one:
for (int childPos = 0; childPos < myLayout.getChildCount(); childPos++) {
myLayout.getChildAt(childPos).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ...
}
});
}
This may help
LanearLayout parentLayout = (LinearLayout)findViewById(R.id.main_ll);
int count = parentLayout.getChildCount();
for (int i = 0; i < count; i++)
{
View view = mainLl.getChildAt(i);
if (view instanceof TextView)
{
int id=view.getId();
((TextView) view).setText(" ");
}
}

Related Links

difference between mAdded & mActive in source code of support.FragmentManager
Google App Engine Custom SSL & Android SNI + VIP?
Android - ImageView flickering by changing the Image (Bitmap) on it
Android: Picasso load image failed . how to show error message
Is it possible to integrate a gradle Library project to an android Project
How to integrate Navigation drawer with out using support library
Unable to apply function on Listview item button click and inset data in database
I used this code below to simple display multiple images from links but application closed
Disable popup in select box in android device
How to run the Wear app with handled app with out any signing? I mean developer debug mode
Default Android host files block Google Analytics?
Marmalade deployment to android: Icon and caption settings have no effect
Android Gridview scrolling up and down quickly messes up the view
AndroidViewClient remove package and broadcastreceiver
How to detect nearby android devices using the same app
How to rotate a particular fragment activity using tabhost in android

Categories

HOME
hyperlink
amp-html
jelastic
access
rom
alsa
mips
zip
jetty
jquery-ui
jax-ws
reactive-programming
openpyxl
selenium-ide
poedit
r-mice
imageresizer
zxing
jquery-callback
onload
maze
syntax-highlighting
jackson-modules
jasmine-node
exchangewebservices
failover
complex-numbers
jbutton
quickfixj
contacts
installscript
servicestack-text
searchkit
odroid
outputcache
contract
xmldocument
mesosphere
lampp
automapper-5
netbeans6.8
vugen
tinymce-3
manova
context-switch
backbone-collections
usart
docpad
google-cloud-console
iotivity
flex4.6
macaulay2
jboss-tools
android-wake-lock
query-by-example
contentcontrol
crouton-os
libvlc
vhosts
rewrite
oracle-spatial
opserver
toolkit
linked-tables
dnssec
zend-currency
phpldapadmin
iiop
ssis-data-flow
sip-server
jama
named-parameters
ignite-ui
lambda-architecture
wxformbuilder
observium
neos-server
joomla-template
adobe-media-server
jdb
httponly
mysql-error-1205
creole
tmx
metaweblog
squishit
easyb
pos-for-.net
request-headers
business-model
wcf-proxy

Resources

Encrypt Message