Get Values From Group Layout in Custom ExpandableListView OnChildClick - android

So if in
void ChildClick(object o, ExpandableListView.ChildClickEventArgs e)
o is the ExpandableListView that was clicked and
e.Parent is also the ExpandableListView that was clicked and
trying to create an instance of the LinearLayout in the Group for which the ChildClick took place by doing
ExpandableListView view = o;
LinearLayout group = (LinearLayout) view.GetChildAt(e.GroupPosition);
returns the LinearLayout in the nth position and not the GROUP layout in the nth position, how do I get an instance of the LinearLayout in the Group for which the child was clicked?

I'm not really sure about where in the View tree you are trying to get too.
But once you have a handel into that tree you can always call
view.getParent();
to go up a level. And
view.findViewById(R.id.yourlayoutname);
to go do several layers

So this seems like going around my elbow to get to my ass instead of just having the parent of the child group actually be the parent group instead of the list view itself, but I've been able to get what I needed thusly:
ExpandableListView view = (ExpandableListView) o;
IExpandableListAdapter mAdapter = view.ExpandableListAdapter;
string s = mAdapter.GetGroup(e.GroupPosition).ToString(); //returns comma separated string with all my values from the group layout in the clicked item group position.
string[] items = s.Split(new [] {","}, StringSplitOptions.None);
Still open for suggestions if there is a way to get an instance of the actual group layout rather than just hoping the string in mAdapter always has the same number of comma delimited strings and the one I am after is always in the same spot. I want to get the actual LinearLayout in group position n so I can find the TextView by ID inside of it.

Related

How to dynamic insert elements of a string array into textview and position it?

I get the list of elements from the HTTP response, then I want to dynamically insert that list into the textview inside the "box" that you can see, currently it just inserts a string and overlaps them one over the other. I tried changing the layout (all three constraint, relative and linear) and it didn't help. Does anyone know how to position them dynamicly inside the boxes and not overlap but have margins like in the second picture? Otherwise, inside the project, I use a constrain layout.
Here is my code:
RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.layout);
int size = response.toArray().length;
final TextView[] tv = new TextView[size];
TextView temp;
for (int i = 0; i < size; i++)
{
temp = new TextView(Activity.this);
temp.setText(response.get(i).getName());
parentLayout.addView(temp);
tv[i] = temp;
}
Here is the picture how it looks right now:
And here is the picture how I want it to looks like:
This sounds like a typical ListView use case.
Firstly, I'd suggest you go through the documentation -
https://developer.android.com/reference/android/widget/ListView
You can see an implementation example of a list view with an array of strings here -
https://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65
In general, you choose the UI of your item and the listView populates the view to each item in your list (each string in your case).
In the adapter, you give each item the data it needs for the UI.
I would suggest you to use RecyclerView for this type of task. You may use ListView as well.
But RecyclerView is more flexible and advanced than ListView.
Create a simple layout or xml file for your row item to be shown in RecyclerView.
Add that row xml file in onCreateViewHolder method. And inside method onBindViewHolder do necessary task like for example, showing name in the list for each position.
Go to this link for your reference : https://developer.android.com/guide/topics/ui/layout/recyclerview
Instead of Array<String> you can use Array<CustomModel> as well depending on your requirement.
Simple example of RecyclerView with model objects as list : https://www.javatpoint.com/android-recyclerview-list-example
Well, the proper way to do what you need is use ListView or RecyclerView.
Anyway, if you want to use your current solution, you need to specify the position of each TextView.
For example, assign an ID to each textview you create and then set the position of it under the previous one. Here you can find how to do that.

How to show a list of items with 2 fields?

I'm developing my first app and I want to show a grid in which there is a list of entries. I get those entries through queries on local SQLite database so this it is a dynamic list. Every item of this list should have 2 field: a string and a value.
How to correctly do it in an activity?
I see ListView but it doesn't seem to suit my needs and I do not need clickable items.
Can you suggest a better solution?
I would still use a ListView.
They don't have to be clickable, you can customise the view to have 2 columns (or to be whatever you want) and they have great performance benefits with view recycling, etc during scrolling.
Essentially you need to return a custom view as the row - this row view will have data aligned horizontally and so you can get columns.
Here is an example.
Create programatically or in XML LinearLayout with vertical orientation:
LinearLayout layout = findViewById(r.id.layout);
layout.setOrientation(LinearLayout.VERTICAL);
Then create Textview for every item with text consisting of string and value of item and add them to the layout:
for (int i=0; i<items.size; i++) {
TextView tv = new TextView(context);
tv.setText(items[i].stringName + ": " + items[i].value);
layout.addView(tv);
}

Change TextView on multiple parts of XML layout

I am adding multiple XML Views programmatically. I'm using a layout inflater to add them and there are no problems with that.
But I'm not able to modify the TextView in each of them.
For example, consider I am adding a LinearLayout three times in my final View. I have a TextView in that linear layout. I extract it using findViewById and if I setText("hello"); it is being reflected in the first layout, but not the second and third.
Will the inflater create new ids dynamically when adding multiple XML elements?
To answer part of your question, no: Ids are not dynamically generated by LayoutInflater.
When you say you are adding the views, where are you adding them? You mention a final View.
You don't include your code, but I assume you are calling Activity.findViewById. If you call this.findViewById from your Activity, you are traversing the entire View hierarchy and finding the first view with such an Id.
What you need to do, is iterate through all of the LinearLayouts that contain your TextViews and call findViewById on each of them.
for (LinearLayout layout : <fill in>) {
((TextView) layout.findViewById(R.id.yourid)).setText("hello");
}
As for the <fill in>. Hard to tell how to fill it in without your code. It seems like you are adding these into a parent view right? Let's assume we have a parent View parent = some view;.
You just have to get all of its children and iterate. There are a few ways you can do it depending on which subclass of View the parent is. Let's keep it simple and assuming the parent is just another LinearLayout.
In that case the for loop changes to something like:
for (int i = 0; i < parent.getChildCount(); i++) {
final LinearLayout layout = (LinearLayout) parent.getChildAt(i);
((TextView) layout.findViewById(R.id.yourid)).setText("hello");
}

How to get element ids of inflated xml in listview?

I have a custom listview.In that I want to get ids of widgets used in particular row.Because I want to set the values on a textview click ,which is present in same xml.
How to get element ids of particular row
thanks
See answer at: How to get widget ids of selected row of listview
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.

Finding child views from parent view created programmatically in Android

For an Android App, I'm using a GridView and extending BaseAdapter to organize its contents. For the function getView that I override in my extended BaseAdapter class, I create a LinearLayout, which I attach an ImageView and 3 TextViews to. Now, I need to implement convertView, but because I created my views programmatically, I didn't think I can use findViewById to find these child views to change their properties (like text and bitmaps).
I had the idea of assigning a unique ID pertaining to different types of views for each one when I create them (example: give my name textview the id of 1, description textview the id of 2, etc), but I was not sure if the ids have to be unique among every view, whether they're the same kind of view or not.
Ultimately, how do I find a child view that's part of a linearlayout view which were all created programmatically?
You can get children of LinearLayout via getChildAt() but it's hard to distinguish what child exactly you get.
I think assigning IDs is better way. You can assign IDs to your views and later get views via findViewById(). IDs doesn't need to be unique for each LinearLayout.
Something like this:
// Give more sensible names to ID's
int IMAGEVIEW_ID = 0;
int TEXTVIEW1_ID = 1;
int TEXTVIEW2_ID = 2;
int TEXTVIEW3_ID = 3;
imageView.setId(IMAGEVIEW_ID);
textView1.setId(TEXTVIEW1_ID);
textView2.setId(TEXTVIEW2_ID);
textView3.setId(TEXTVIEW3_ID);
...
// somewhere later
ImageView imageView = (ImageView) convertView.findViewById(IMAGEVIEW_ID);

Categories

Resources