In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use the AdapterView components of Android". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Overview
In Android application development, AdapterView is a kind of common and very important component. Our common component that displays information in the form of a list is a subclass of AdapterView, called Listview;. The component that we often browse thumbnails of pictures in a grid way is also a subclass of AdapterView, and it is called GridView; drop-down list to show optional components are also subclasses of AdapterView, called Spinner; and so on. They are all subclasses of AdapterView.
Introduce the programming mode of AdapterView
Use the ListView component of Android to display in a list all the program information that has been installed on the Android system. ListView, as its name implies, is to present information to users in the form of lists. Just like the list in your phone's settings, it contains the icons of all your applications, the application name (similar to the figure below) and the class name of the entry Activity. When the content is displayed beyond the available area of the physical screen, it can also scroll, just like the ScrollView we talked about last time.
Such as the following figure: the large yellow box is a ListView, and the dark blue box is a list item in a list, so in the program to use ListView to display information, you must do the work.
(1) include a ListView component in the interface layout
(2) layout the list items displayed in the list
(3) Design a class that implements the Adapter interface to provide the data that needs to be displayed for the ListView component.
Adapter
The list components (ListView), grid components (GridView) and drop-down list components (Spinner) just mentioned are subclasses of Adapter. These components are only responsible for displaying data, and the data to be displayed must be managed through an interface called Adapter. Taking the use of ListView to display data as an example, the relationship between AdapterView and Adapter interfaces is shown below:
Common methods of Adapter and their meanings:
Method name meaning int getCount () returns the total number of data in the dataset to be displayed Object getItem (int position) returns the data object long getItemId (int position) at the specified location in the dataset, returns the ID of the data at the specified location in the dataset
View getView (int position
View convertView
ViewGroup parent)
Build the data of the specified location into a component that can be displayed in AdapterView, and return to AdapterView for display ListView use
Activity_main.xml
Select New → Layout resoure file to create
Item.xml
MyAdapater.javapackage com.example.demo03_22; import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView; public class myAdapater extends BaseAdapter {private String [] books= {"Java programming", "Android Application Development", "oracle Database Management Guide", "JavaWeb programming", "the Road of system Engineers in Software Engineering"; LayoutInflater inflater; int id_item Public myAdapater (Context context,int id_item) {this.id_item=id_item; inflater= (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);} @ Override public int getCount () {return books.length;} @ Override public Object getItem (int I) {return books [I];} @ Override public long getItemId (int I) {return I } @ Override public View getView (int I, View view, ViewGroup viewGroup) {TextView tv; tv= (TextView) inflater.inflate (id_item,viewGroup,false); tv.setText (books [I]); return tv;}} MainActivity.javapackage com.example.demo03_22; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;import android.widget.ListView Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); ListView lv= (ListView) this.findViewById (R.id.lv1); myAdapater myAdapater=new myAdapater (this, R.layout.item); lv.setAdapter (myAdapater);}}
Test results:
Improvement: adding pictures
Activity_main.xml
Item.xml
MyAdapater.javapackage com.example.demo03_22; import android.annotation.SuppressLint;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView Public class myAdapater extends BaseAdapter {private BookItem [] books= {new BookItem ("Chen SB", R.drawable.dog), new BookItem ("Zhou SB", R.drawable.dog), new BookItem ("R.drawable.dog"), new BookItem ("Lin SB", R.drawable.dog), new BookItem ("Tao SB", R.drawable.dog)}; LayoutInflater inflater; int id_item Public myAdapater (Context context,int id_item) {this.id_item=id_item; inflater= (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);} @ Override public int getCount () {return books.length;} @ Override public Object getItem (int position) {return books [position];} @ Override public long getItemId (int position) {return position } @ SuppressLint ("ViewHolder") @ Override public View getView (int position, View view, ViewGroup parent) {LinearLayout LL= (LinearLayout) inflater.inflate (id_item,parent,false); ImageView iv= (ImageView) LL.findViewById (R.id.book_phone); iv.setImageResource (books[ position] .photo); TextView tv; tv= (TextView) LL.findViewById (R.id.book_name) Tv.setText (books[ position] .name); return LL;} / * * define a picture class * / private class BookItem {String name; int photo; public BookItem (String name,int photo) {this.name=name; this.photo=photo;}} MainActivity.javapackage com.example.demo03_22; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle Import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); ListView lv= (ListView) this.findViewById (R.id.lv1); myAdapater myAdapater=new myAdapater (this, R.layout.item) Lv.setAdapter (myAdapater); lv.setOnItemClickListener (this);} @ Override public void onItemClick (AdapterView parent, View view, int position, long id) {TextView textView= (TextView) view.findViewById (R.id.book_name); String name= (String) textView.getText (); String text= "make sure to choose" + name+ "do you want to play hot pot tonight? Toast.makeText (this,text,Toast.LENGTH_LONG). Show () This is the end of the content of "how to use the AdapterView components of Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.