having trouble inflating view in custom expandable list adapter - java

I created a custom expandable list adapter and inside the getGroupView()
and getChildView() i do the following but recieve an error:
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.expandable_list_parent_row_layout);
}
return convertView;
}
compiler tells me that it cannot find method - getActivity()
Can someone please help? Thanks!

In the adapter constructor, you will have a Context argument. Use this (Activity)context to getLayoutInflater.
Adapter class does not have getActivity() method.

You need to post more of your code - I suspect your accessing this in an Activity and not a Fragment (which provides the method, getActivity())
You need your Context in the code, this is what your code should look like.
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);

Related

Why creating a new variable and using it when you can use the old variable?

Why do we need to :-
Create a View x.
Then set x = a
Then use a if command on x if you can directly use a.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// check if the current view is reused else inflate the view
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Instead , Why can't we do this?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
First and second examples are valid. Only if first case you are creating an local copy of your class variable. It is useless. Why are developers doing this way? Who knows :)
About inflating. Inflate operations some expensive, cause your adapter items are simular, it is possible to inflate view only once.
The second option also works perfectly. I don't know why you think you can't do that.
Just make sure you return convertView after doing other stuffs inside there.
As the develeoper in some cases wanted to assign a value LayoutInflater.from(...).inflate(...), different from the argument convertView, he chose not to overwrite the argument, but to introduce a new variable. It's good style not to modify method arguments.
So, in the case that convertView is null, listItemView gets a value from the LayoutInflater call, to be used further down the method. And the fact that the method was called with a null argument is still visible.
As a more concise alternative, this can be done using Java's ternary operator:
View listItemView = convertView != null ?
convertView :
LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
This way the variable can even be declared final.

Bad casts of object references: unchecked/unconfirmed cast

Consider the code below:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView; //here
if (imageView == null) {
imageView = (ImageView) inflater.inflate(R.layout.item_gallery_image, parent, false);
}
ImageLoader.getInstance().displayImage(IMAGE_URLS[position], imageView, options);
return imageView;
}
Findbugs plugin from Android Studio is complaining about the first line from the method getview, it says:
Unchecked/unconfirmed cast
This cast is unchecked, and not all instances of the type casted from can be cast to the type it is being cast to. Check that your program logic ensures that this cast will not fail.
Any ideas of how do I resolve this issue?
If you want to please FindBugs you can assert that convertView is an ImageView.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_gallery_image, parent, false);
}
assert convertView instanceof ImageView : convertView;
ImageLoader.getInstance().displayImage(IMAGE_URLS[position], (ImageView) imageView, options);
return imageView;
}
Not every View is an ImageView, so FindBugs is trying to warn you that the cast may result in a ClassCastException.
However, you actually can't get a ClassCastException because the way convertView works is that you either recieve null or you receive a View previously returned by the getView() method. Since your method always returns an ImageView, everything is fine.
FindBugs finds potential problems with your code, and sometimes they are actually not problems at all.
i think you try to fill every row of a listView or recycle view with image...first you should make layout that contains an image view then you can do it in this way:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view=inflater.inflate(R.layout.adapter_row,parent,false);
ImageView mImage=(ImageView) view.findViewById(R.id.imageid);
ImageLoader.getInstance().displayImage(IMAGE_URLS[position], mImage, options);
return view;
}

Listview with color and button android

I need to customize this listview, I wonder how I could do to put the buttons and an identifier of different colors to distinguish them.
Anyone know how it could be the xml? and how it could change colors ?? is there any component in android? or I could use an imageview to the colors?
I appreciate your help.
You should use a custom class, to create the Adapter for the List, and that class has to extend the BaseAdapter class. In this class, you have to implement the following methods:
public int getCount()
public Object getItem(int position)
public long getItemId(int position)
public View getView(int position, View convertView, ViewGroup parent)
In the last one, you can get a LayoutInflater, and load a view from .xml. In that .xml, you can define the layout of one row. Here's an example from one of my projects:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// menuItems is an ArrayList of Strings
final String menu = menuItems.get(position);
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_mainmenu_row, null);
TextView menuText = (TextView) view.findViewById(R.id.menuListRow_menuItem);
menuText.setText(menu);
return view;
}

Highlight items of a listview without using onTouch or onItemClick

I am using a default array adapter to display my listview.
How do I highlight specific rows from my listview without touching anything i.e. without using onTouch() or onItemClick() (Just by using a code!) ?
I guess the way you should do it is use custom adapter and in getVew function highlight the row you needed
e.g.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final Holder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
...
if(position==thePositionYouNeed)
row.setBackgroundColor(color)

Can I populate a listview from getView of a custom adapter?

I have a listview that contains items that have listviews. I am trying to populate item's listviews from inside the getView of the custom adapter that populates the "parent" listview:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
System.out.println("session adapter: here1");
if (v == null) {
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_session, null);
}
SessionObject i = sessions.get(position);
if (i != null) {
tvTrackName = (TextView) v.findViewById(R.id.textViewTrackName);
tvTrackName.setText(i.trackName);
tvSessionName = (TextView) v.findViewById(R.id.textViewSessionName);
tvSessionName.setText(i.sessionName);
tvSessionModerator = (TextView) v.findViewById(R.id.textViewModeratorName);
tvSessionModerator.setText("Moderator: "+i.sessionModerator);
listAbstracts = i.abstractList;
lvAbstracts = (ListView) v.findViewById(R.id.listViewAbstracts);
AbstractObjectAdapter adapter = new AbstractObjectAdapter(act, R.id.listViewAbstracts, listAbstracts);
lvAbstracts.setAdapter(adapter);
}
return v;
}
The "interior" adapter seems to only be calling its getView function once, regardless of the number of items in its list. If there are 2 items, only the first gets put into the listview. Is this the wrong way to do this? Am I missing something?
Any help is appreciated. Thanks.
Just create your item view dynamically, adding textviews to a linearlayout. You'll just have to be careful about handling the convertView, initially it will be easiest to always ingore it but you'll take a bit of a performance hit.
Optimise later.

Categories