Cannot resolve symbol "ConvertView" - android

I'm following this tutorial to implement Expandable ListView: https://www.youtube.com/watch?v=GD_U0-N3zUI&t=404s
I did everything same as in it, But:
#Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String title=(String)this.getGroup(i);
if(convertView==null){
LayoutInflater layoutInflater=(LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.parent);
}
return null;
}
I'm getting this error: "Cannot resolve symbol convertView"
Tried:

I did everything same as in it
No you didn't. If you watch the video closely you will notice the parameter names are different in the video.
I'm getting this error: "Cannot resolve symbol convertView"
That is because convertView is not defined in your code. (in the video it is).
You can fix it by either changing the parameter names:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
The key here (for your problem) is the third parameter View convertView.
The other option is to use the your variable name view everywhere convertView is used.
Update: the inflate function expects a layout resource id (e.g. R.layout.parent an a corresponding XML file should exist in the rea/layout folder) as the first parameter. You are passing an ID resource.

Simply replace your code with below:
#Override public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup)
{ String title=(String)this.getGroup(i);
if(convertView==null){ LayoutInflater layoutInflater=(LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.parent, null); } return convertView; }

Related

ExpandableListView with seekbar doesn't expand

I've made a custom BaseExpandableListAdapter. I'm trying to display a seekbar on some (not all) group views. To do this, I'm inflating a custom layout with a seekbar in it wich I hide/unhide depending on the group.
The problem is that these group views WITH VISIBLE seekbar doesn't expand. The ones with HIDDEN seekbar do expand. All group views have children.
This is my getGroupView
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(com.voy.sima.R.layout.itemcomplejo, parent, false);
}
Complejo entry = (Complejo)getGroup(groupPosition);
SeekBar sbDesarrollo = (SeekBar) convertView.findViewById(R.id.sbDesarrollo);
sbDesarrollo.setVisibility(View.INVISIBLE);
if(entry.getId() == 2) {
sbDesarrollo.setVisibility(View.VISIBLE);
}
}
What am I missing? I'm sure it's something silly but it's driving me crazy.
There is open source project from Google android developer for expanding Listview with animation.
Here is the link: http://developer.android.com/shareables/devbytes/ListViewExpandingCells.zip
And this is an explanation on YouTube of this project.
I hope this could help you ..
I've figured it out. Adding sbDesarrollo.setFocusable(false); did the trick.
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(com.voy.sima.R.layout.itemcomplejo, parent, false);
}
Complejo entry = (Complejo)getGroup(groupPosition);
SeekBar sbDesarrollo = (SeekBar) convertView.findViewById(R.id.sbDesarrollo);
sbDesarrollo.setVisibility(View.INVISIBLE);
if(entry.getId() == 2) {
sbDesarrollo.setVisibility(View.VISIBLE);
sbDesarrollo.setFocusable(false);
}
}

Different icon to items in expandiblelistview

Is it possible to assign different icon to each item in a group? Can anyone show me an example? I would like a result like this:
Thank you.
From the API reference of ExpandableListAdapter:
getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent):
Gets a View that displays the data for the given child within the given group.
This is the method that returns the View of the child, which means you could write something like this in your ExpandableListAdapter:
#Override
public View getChildView(int gPos, int cPos, bool isLast, View view, ViewGroup parent){
if (view == null){
view = inflater.inflate(R.layout.whatever, null);
}
ImageView image = (ImageView) view.findViewById(R.id.image);
// This next part depends on how you store the images.
// You could store the imageID in an int[][]:
int imageID = carImages[gPos][cPos];
image.setImageResource(imageID);
// or you could store the drawable in a List<List<Drawable>>
Drawable d = carimages.get(gPos).get(cPos);
iamge.setImageDrawable(d);
// etc.
return view;
}
The point is that you have to store the images in a way that you can retreive the right image by providing which index the view has, and in which group it is in.
You could also store the image in a CarObject, and have a List<CarObject> in your adapter. But you would have to find a way to distinguish between the different brands.

how to get view of group in expandablelistview in android?

im blazilian so, my english is not good.
So.. i need get view of group in expandablelistview for get your object tag throught view.getTag() method.
follow me in this example:
ExpandableListView
--> group (i need this view)
----> child
----> child
----> child
--> group (i need this view)
----> child
----> child
My code:
#Override
public boolean onChildClick(final ExpandableListView parent, final View v,
final int groupPosition, final int childPosition, final long id) {
/* I NEED GET VIEW OF GROUP FOR GET YOUR TAG*/
View vParent = parent.getChildAt(groupPosition); // dont work after first group
Programa v2 = (Programa) parent.getTag(); // return null
// v parameter is a child of group
return true;
}
in my adapter:
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TwoLineListItem view = (TwoLineListItem) LayoutInflater.from(contexto)
.inflate(android.R.layout.simple_expandable_list_item_2,
parent, false);
String programa = map.keySet().toArray(new String[map.keySet().size()])[groupPosition];
view.getText1().setText(programa);
view.getText2().setText("PROGRAMA LOCAL");
view.setTag(programas.get(groupPosition)); // i need get this in child click listener
return convertView = view;
}
any idea? thanks
To get the group view from an ExpandableListView, you do something as follows:
public View getGroupView(ExpandableListView listView, int groupPosition) {
long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition);
int flatPosition = listView.getFlatListPosition(packedPosition);
int first = listView.getFirstVisiblePosition();
return listView.getChildAt(flatPosition - first);
}
If your code is correct, you want to get the group view for the child so that you can call getTag() on it, correct?
If so, why not just skip that step and access the value of the tag, set by programas.get(groupPosition) manually?
You can do this by calling:
programas.get(groupPosition) right on the top of your onChildClick method since you get the group position value there too.
Edit in response to your comment:
The issue here is that you're not going to be able to get the view of the group through the adapter since it might involve recreating the view due to recycling of views in lists. If this method doesn't work, I strongly suggest modifying your code to make it work.
If programas is one of the inputs to your adapter, then call getGroup(groupPosition) on your adapter to access it. Else, make a public method in your adapter class to allow retrieval of that value.
I tried AedonEtLIRA answer but it only obtained the first item in the list.
My solution was to set a tag in my adapter's getGroupView method.
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listitem_group, null);
}
//This is your data object that you might be using for storage that can be returned by your getGroup(groupPosition) function
GroupDataDto groupDataDto = (GroupDataDto) getGroup(groupPosition);
String name = groupDataDto.getName();
convertView.setTag(name);
...
Now in your Fragment / Activity, in your onGroupExpand and onGroupCollapse:
GroupDataDto groupDataDto = (GroupDataDto) listAdapter.getGroup(groupPosition);
ImageView arrow = (ImageView) getView().findViewWithTag(groupDataDto.getName()).findViewById(R.id.expandable_arrow_down);
Then I can do my animation on the arrow, which is why I wanted to get that group view.
Well I used the following code to access a TextView in a group:
#Override
public boolean onChildClick(final ExpandableListView parent, final View v,
final int groupPosition, final int childPosition, final long id) {
View vParent = mAdapter.getGroupView(groupPosition, true, null, null);
Programa v2 = (Programa) vParent.getTag();
return true;
}
I hope it helps

How to get the spinner id or tag from getDropDownView method in android

I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.
The custom array adapter looks like:
class BackgroundColorAdapter extends ArrayAdapter<String> {
BackgroundColorAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
if(parent.getTag().equals("background"){
//Do custom stuff here
}
return(row);
}
}
and I'm setting the tag:
settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());
I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?
edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing
Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.
class TextSizeAdapter extends ArrayAdapter<String> {
TextSizeAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
TextView text = (TextView)row.findViewById(R.id.item_text);
text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
if(settingsSpinners[2].getSelectedItemPosition() == position){
radio.setChecked(true);
}else{
radio.setChecked(false);
}
return(row);
}
}
I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:
parent the parent that this view will eventually be attached to
This doesn't sound like the 'parent' you are looking for...
Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:
public Spinner mParent = null;
public View getView (int position, View convertView, ViewGroup parent)
{
this.mParent = parent;
return super.getView(position, convertView, parent);
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // Your code here -> but use 'mParent'
}
I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.

Creating View manually

How can I create convertView manually in following method. I read that convertView can be created manually or inflated from xml file.
public View getView(int position, View convertView, ViewGroup parent)
I am using xml file for layout.
Use View.inflate(context, resource, root);
#Override
public View getView(int position, View convertView, ViewGroup parent){
final View contentView = convertView != null ? convertView : View.inflate(context, resource, null);
}
where resource is your xml layout resource id like R.layout.list_item.
Something like that.
You can use this code inside the override getView method while extending the ArrayAdapter Class
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
R.layout.list_item is the resource to create the view based on.
the parameter "false" is false to weather to attach to the root.

Categories

Resources