Why use inflater in listview

  • I always had an ambiguity in why we need to use inflater in android, why are they used in ListView for custom layouts (like below)?
  • What is Inflater?
  • What is the advantage of using Inflater?

public class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; public MobileArrayAdapter(Context context, String[] values) { super(context, R.layout.list_mobile, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.list_mobile, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); textView.setText(values[position]); 

Thanks,

+6
source share
3 answers

What is Inflater?

To summarize what the LayoutInflater Documentation says ... A LayoutInflater is one of the Android system services that is responsible for receiving your XML files, which define the layout, and turning them into View objects. The OS then uses these view objects to draw a screen.

I always had an ambiguity in why we need to use inflater in android, why are they used in android ListView for custom layout?

Usually you do not need to use LayoutInflater directly. Android does most of the layout inflation for you when you call setContentView() in the onCreate() method of your activity. Thus, you, as a programmer, are responsible for ensuring that opinions are inflated. . Now you want to inflate the views in the context of the ListView. The Adapter class can do inflation for you if you don't want to customize each element. But if you want to customize the views shown in the list, you will have to manually inflate each view with LayoutInflater, since there is no other existing method that you can use.

What is the advantage of using Inflater?

It makes no sense to use it. You must use LayoutInflater in some form or form to inflate your static XML layouts.

Alternatively, you can dynamically create views using java code. However, you will need to call methods for each property for manual submission. In my opinion, it’s easier to use the XML / inflation process. In addition, Android preprocesses your XML files at build time, so this leads to faster execution time.

+13
source

I always had an ambiguity as to why we need to use inflater in android, why are they used in android ListView for custom layout?
They are used to create views for each row.

What is Inflater?
A system service that creates a view from an XML layout.

In the code below, I am trying to understand why Inflator is used?
The supercharger is used to create views for strings.

What is the advantage of using Inflater?
Compared to what? How do you want to create views from an XML layout?

+3
source

Simply put, a cheater allows you to create a view from a resource layout file, so you don't need to create everything programmatically.

In your example, you are inflating the layout of R.layout.list_mobile . This allows you to access all the views in it. For example, you call:

 TextView textView = (TextView) rowView.findViewById(R.id.label); 

By calling rowView.findViewById() , you can access the views that were created in this layout. Often for ListViews, you will have an XML string file, which then inflates and puts your data in the views.

0
source

Source: https://habr.com/ru/post/950921/


All Articles