I basically have 3 different objects in my list view:
TextView1
TextView2
TextView3
I want to get the object ID from the list view that's created dynamically. Ex: How do I set TextView2 with a background image from a list view in position 1?
I've tried using
lv.getItemAtPosition(1);
This will return the whole row and I'm just looking for the object ID TextView2 inside lv.getItemAtPosition(1)? Once I get the objectID from a certain position in list view, I can than change the TextView2 background.
Sorry, if I didn't explain clear enough. Does anyone know what I'm talking about?
If you write about row, I assume something like that can work for you:
TableRow tableRow = lv.getItemAtPosition(1);
for (int i = 0; i < tableRow.getChildCount(); i++) {
View child = tableRow.getChildAt(i);
if ( child instanceof TextView ) {
TextView textView = (TextView) child;
textView.DO_SOMETHIG__WITH_TEXT_VIEV();
textView.requestLayout();
}
}
tableRow.requestLayout();
Of course if you have some other row than tableRow, you can try to change it to that type.
Related
I have a custom view. I am trying to add multiple arrray values. But its just printing the last one. For instance check the below code. I am trying to dispay schedulePeriod.scheduleMatchups.get(i). It has two values. (get (0) and get (1). Its always getting the value of get(1) not get(0). scheduleView.add ( is returning one value not both) can anyone help me?
Thank You
private ArrayList<View> getScheduleView(final SchedulePeriod schedulePeriod) {
String teamid = FantasyFootballApplication.getFantasyTeam().teamId;
View scheduleView = playerRankingsInflate(R.layout.schedule_view);
ArrayList<View> scheduleViews = new ArrayList<View>();
for(int i=0; i<schedulePeriod.scheduleMatchups.size(); i++){
if(!(schedulePeriod.scheduleMatchups.isEmpty())) {
TextView weekNumber = (TextView) scheduleView.findViewById(R.id.week_number);
weekNumber.setText(schedulePeriod.scheduleId);
if(!(teamid.equals(schedulePeriod.scheduleMatchups.get((i)).scheduleAwayTeam.awayTeamId))) {
TextView scheduleOpp = (TextView) scheduleView.findViewById(R.id.schedule_opp);
scheduleOpp.setText(schedulePeriod.scheduleMatchups.get(i).scheduleAwayTeam.awayTeamShortName);
TextView scheduleName = (TextView) scheduleView.findViewById(R.id.schedule_teamname);
scheduleName.setText(schedulePeriod.scheduleMatchups.get(i).scheduleAwayTeam.awayTeamName);
} else if(!(teamid.equals(schedulePeriod.scheduleMatchups.get((i)).scheduleHomeTeam.homeTeamId))) {
TextView scheduleOpp = (TextView) scheduleView.findViewById(R.id.schedule_opp);
scheduleOpp.setText(schedulePeriod.scheduleMatchups.get(i).scheduleHomeTeam.homeTeamShortName);
TextView scheduleName = (TextView) scheduleView.findViewById(R.id.schedule_teamname);
scheduleName.setText(schedulePeriod.scheduleMatchups.get(i).scheduleHomeTeam.homeTeamName);
}
}
scheduleViews.add(scheduleView);
}
return scheduleViews;
}
As I can see you are creating only one view and adding it to list in for loop. And therefore you are editing only this view and you see last value in your scheduleMatchups.
You want to place your
View scheduleView = playerRankingsInflate(R.layout.schedule_view);
inside the for loop and then alter every view according and add it to your list.
Hope this helps and enjoy your work.
I have a LinearLayout with many nested LinearLayouts and TextViewss
My main activity inflates the main LinearLayout,
Then I load data from a server and based on the data received, I add multiple Layouts in a place holder (LinearLayout)
This is simple a news page where I load Images associated with the news and place it inside an initially empty LinearLayout.
Each Image has the following info: Title(TextView), Date(TextView), Image(ImageView) so what I actually do is the following:
*Please notice that this is only the essential coded in the question I elemenated all the try -> catch ... if/else ....etc
public void addImages(JSONArray images){
ViewGroup vg = (ViewGroup) findViewById(R.id.imagesPlaceHolder);
// loop on images
for(int i =0;i<images.length;i++){
View v = getLayoutInflater().inflate(R.layout.image_preview,vg);
// then
I think that here is the problem
ImageView imv = (ImageView) v.findViewById(R.id.imagePreview);
TextView dt = (TextView) v.findViewById(R.id.dateHolder);
TextView ttl = (TextView) v.findViewById(R.id.title);
// then
dt.setText("blablabla");
ttl.setText("another blablabla");
// I think the problem is here too, since it's referring to a single image
imv.setTag( images.getJSONObject(i).getString("image_path").toString() );
// then Image Loader From Server or Cache to the Image View
}
}
The code above works good for a single image
But for multiple images the Image Loader doesn't work I guess it's because all ImageViews (Inflated multiple times) have the same ID
When you provide a ViewGroup to be used as the parent, the View returned by inflate() is this parent (vg in your case) and not the newly created View. Therefore, v points toward the ViewGroup vg and not toward the newly created View and as all of your children have the same id, the same subviews (imv, dt, ttl) are returned each time.
Two solutions. The first one is to change their id right after you are finished with them, before the next iteration. Therefore, on the next creation at the beginning of the next iteration, the newly created Views will have a different IDs from the older Views because they will still use the old constant defined in R.
The other solution would be to add the parameter false to the call to inflate() so that the newly created view will not be attached to the ViewGroup and will then be returned by the inflate() function instead of the ViewGroup. The rest of your code will then works as attended with the exception that you will have to attach them to the ViewGroup at the end of the iteration.
Notice that you still need to provide a ViewGroup because it will be used to determine the value of the LayoutParams.
I had the same problem, and based on the answer from #SylvainL, here'a a working solution:
// myContext is, e.g. the Activity.
// my_item_layout has a TextView with id='text'
// content is the parent view (e.g. your LinearLayoutView)
// false means don't add direct to the root
View inflated = LayoutInflater.from(myContext).inflate(R.layout.my_item_layout, content, false);
// Now, before we attach the view, find the TextView inside the layout.
TextView tv = (TextView) inflated.findViewById(R.id.text);
tv.setText(str);
// now add to the LinearLayoutView.
content.addView(inflated);
Is there a reason why the ImageView in the layout XML needs to have an ID? Could you erase the android:id attributes from the image_preview.xml layout and then simply iterate through the children of the inflated LinearLayout? For example:
ViewGroup v = (ViewGroup)getLayoutInflater().inflate(R.layout.image_preview,vg);
ImageView imv = (ImageView) v.getChildAt(0);
TextView dt = (TextView) v.getChildAt(1);
TextView ttl = (TextView) v.getChildAt(2);
I inflate XML-Layout with dynnamic and get text of id
private val onAddView = View.OnClickListener {
val parent = viewForm.findViewById<LinearLayout>(R.id.layout_parent)
LayoutInflater.from(activity).inflate(R.layout.layout_child, parent) // layout_child has id "tv_attribute"
}
private val onSave = View.OnClickListener {
val parent = viewForm.findViewById<LinearLayout>(R.id.layout_parent)
for (i in 0 until parent.childCount) {
val getText = parent.getChildAt(i).findViewById<TextView>(R.id.tv_attribute).text
}
}
Is this a TableLayout? If it is, how to make this underline under first row and different color per row?
Yes this is the table layout you have to set background color of the table row please refer for following link click here
Assuming it is derived from a ViewGroup (object containing children, for example TableLayout or ListView), it is easy to access all of its children (rows) and do something with it. For example alternating backgrounds:
final int childCount = myGroup.getChildCount();
for(int i = 0; i < childCount; i++) {
View child = myGroup.getChildAt(i);
if(i % 2 == 0) {
child.setBackgroundColor(color1);
} else {
child.setBackgroundColor(color2);
}
}
Same goes for changing the first row, just use myGroup.getChildAt(0) and modify that particular child.
You can use listview with custom item view. Just add header in listview (also footer can be added):
ListView list = (ListView) findViewById(R.id.listView);
View headerView = inflater.inflate(R.layout.header, list, false);
list.addHeaderView(headerView);
Yes it is a Table layout
i have created the similar very easily.
You can also add click events for the text in each row to perform different action.
I am using an custom listview in my application.In my row I have (two images,remaining textviews).
when I click on one of textview,I want to set another textview data.I wrote the code below to do this
likes.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// if(likes.getText().toString().equals("Like")){
TextView t=(TextView)v;
TextView likesnumber1 = (TextView) findViewById(R.id.likesnumber);
// TextView likesnumber = (TextView) convertView.findViewById(R.id.likesnumber);
int i= Integer.parseInt(likescount.get(position));
if(like_or_ulike.get(position).equals("Like")){
Log.e("inlike","like");
like_or_ulike.set(position, "Unlike");
t.setText(like_or_ulike.get(position));
UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"post");
// listView.getAdapter().getItemAt(position);
j=i+1;
String s=Integer.toString(j);
likescount.set(position, s);
likesnumber1.setText(likescount.get(position));
}
else{
Log.e("unlike","unlike");
like_or_ulike.set(position, "Like");
t.setText(like_or_ulike.get(position));
UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"DELETE");
j=i-1;
String s=Integer.toString(j);
likescount.set(position, s);
likesnumber1.setText(likescount.get(position));
}
}
});
How can I get the another textview id of particular row when I click the textview.Is it possible to get the ids of that particular row.
As you want to get hold of the textview in the same row, you can do the following.
with the textview that was clicked get its parent and then using the parentview you can search for your textview:
View parent = (View) textViewThatWasClicked.getParent();
if (parent != null) {
TextView textViewThatYouWantToChange = parent.findViewById(R.id.textViewThatYouWantToChange);
textViewThatYouWantToChange.setText(...);
}
you will need to change the id that you look for to the id you have for that other textview. If you need to set an id you can do this in the xml layout file you used.
I cant follow your code well enough to actually give you the code but this should be enough to get you in the right direction.
If you have custom layout for each row in listview then you can provide id for each of the View in your custom view and then set setOnItemClickListener for your listview and you'll be able to reach each row as a view.
When you have access to your row like a View then you can use view.findViewById and get all of your views from your custom view by their id.
I need to get an dynamically added view position in LinearLayout with vertical orientation.
For example i have 4 TextViews added dynamically on LinearLayout, then i need to change position of text colour at 3rd position will be in different color.How can i achieve it by getting position of added views.
You can do it just like that
ViewGroup parent;
int position;
for(int i = 0; i < parent.getChildCount(); ++i) {
int currentViewId = parent.getChildAt(i).getId();
if(currentViewId == wantendViewId) {
position = i;
}
}
That's (in my opinion) the simplest way
If you always know the number of TextViews in your LinearLayout, you can just use the function getChildAt( int position ). This returns a View which you can then cast to a TextView to be able to perform the desired operations.
If you do not know the number of elements you could set the id of each TextView (in order to be able to identify a particular one) and then run through them like this:
for( View view : myLinearLayout )
if( view instanceof TextView && view.getId().equals( idToSearchFor ) )
//Do what needs to be done.
I see following options:
Declare some id's in resources in form of <item type="id">first</item> and assign them to
views in adding to layout, after that use normal findViewById() mechanism
Assign some tags to views you're adding to a layout via setTag method and after that use findViewWithTag mechanism
Remeber position of your views and use them vie getChildAt method
I got simple option.
suppose you add
View v;//any view
linearlayout.addview(v);//add in layout
While u want to modify view.
simpaly remove old view.
linearlayout.removeView(v);
add new update view object
v-updated new view
linearlayout.addview(v);