Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use global variables and local variables in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to use global variables and local variables in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this Android article on how to use global variables and local variables, let's take a look.

Global variables, as the name implies, are variables that can be called in the entire class or in multiple functions. Also known as external variables. Local variables are variables that can be accessed in a specific procedure or function. It's easy to declare a variable, but when it comes to using it, it's not as simple as you think. As for myself, I often define the use of global variables, but it is because of this that global variables are defined. And a lot of twists and turns.

When using Adapter, that is, the adapter, the adapter is usually used with listView, because a listView will basically have an Item layout of listView. The following scenario is: there will be an ImageView in each Item. When I click on a certain item, I need to change the background color or other background picture for the ImageView of that Item. One of the situations that may occur at this time is your explicit *. You will find that the picture in the third or second item has also changed. This is because you define global variables. The code section is as follows:

Public class Adapter extends BaseAdapter {private ImageView img; public View getView (int position, View convertView, ViewGroup parent) {convertView = mInflater.inflate (R.layout.grouproomlistviewroomitem.null); img = (ImageView) convertView.findViewById (R.id.logo); return convertView;}}

In the above part, ImageView is a global variable. At this point, we need to define ImageView as a local variable.

Public class Adapter extends BaseAdapter {public View getView (int position, View convertView, ViewGroup parent) {convertView = mInflater.inflate (R.layout.grouproomlistviewroomitem.null); ImageView img = (ImageView) convertView.findViewById (R.id.logo); return convertView;}}

At this time, it means the ImageView in each Item. Another situation is that when making a shopping cart, you can click the add and subtract icon to change the number of items in the shopping cart. When you define a quantity num, it must also be defined as a local variable. It would be even better if you can use ViewHolder.

This is the end of the article on "how to use global and local variables in Android". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use global variables and local variables in Android". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report