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 Android Spinner and GridView components

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

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Android Spinner and GridView components. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

one。 Overview:

Spinner is a familiar drop-down box list. Similar to ListView, we must specify an Adapter for the Spinner object. Let's start with the simple use of Spinner.

two。 Realize

MainActivity.java

Package com.example.demo03_29; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;import android.view.View;import android.widget.Adapter;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {TextView choice; ArrayAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) Choice= (TextView) this.findViewById (R.id.choice); Spinner spinner= (Spinner) this.findViewById (R.id.sp1); adapter=ArrayAdapter.createFromResource (this, R.array.habit, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter (adapter); spinner.setOnItemSelectedListener (this) } @ Override public void onItemSelected (AdapterView adapterView, View view, int I, long l) {choice.setText (adapter.getItem (I));} @ Override public void onNothingSelected (AdapterView adapterView) {}}

String.xml under the values package

Demo03-29 10 km night running, mountain climbing, swimming, eating, playing code

Activity_main.xml

Sipnner_down.xml

The layout is simple by placing a LinearLayout, a View to separate components, and a TextView to display the selected results in the LinearLayout. Place a TextView for prompting and a drop-down box Spinner in the nested LinearLayout.

three。 Beautification

MyAdapter.java

Package com.example.demo03_29; import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.TextView; import androidx.annotation.NonNull; public class MyArrayApater extends ArrayAdapter {private LayoutInflater minflater; String [] titles String [] desc= {"integration tools", "high-speed storage, storage at any time", "high-quality resources, all in Xunlei", "integrated development tools", "learning artifacts"}; int [] photos= {R.drawable.p1reparentR.drawable.p2memr.drawable.p3legR.drawable.p4 R.drawable.p5} Public MyArrayApater (@ NonNull Context context, int resource, CharSequence [] objects) {super (context, resource, objects); minflater= (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); titles=context.getResources (). GetStringArray (R.array.habit);} @ Override public int getCount () {return titles.length;} @ Override public View getDropDownView (int position, View convertView, ViewGroup parent) {View v V=minflater.inflate (R.layout.spinnerdown downline parentMagic false); ImageView iv= (ImageView) v.findViewById (R.id.p01); iv.setImageResource (photos [position]); TextView tv01= (TextView) v.findViewById (R.id.t1); tv01.setText (title [position]); TextView tv02= (TextView) v.findViewById (R.id.t2); tv02.setText (standing [position]); return v }}

Modify MainActivity.java

four。 GridView

GridView displays the data as a two-dimensional table, and if there is more data, this component will provide a vertical scroll bar. We use a column to illustrate how to use GridView.

Activity_main.xml

ImgeAdapter.java

Package com.example.demo0330; import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView Public class ImageAdapter extends BaseAdapter {private int [] images= {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3 R.drawable.p4, R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,} Private Context context; public ImageAdapter (Context context) {this.context=context;} @ Override public int getCount () {return images.length;} @ Override public Object getItem (int position) {return images [position];} @ Override public long getItemId (int position) {return position;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView If (convertView==null) {imageView=new ImageView (context); int width= GridView.LayoutParams.MATCH_PARENT; int heght= GridView.LayoutParams.MATCH_PARENT; imageView.setLayoutParams (new ViewGroup.LayoutParams (width,heght)); imageView.setScaleType (ImageView.ScaleType.CENTER_INSIDE);} else {imageView= (ImageView) convertView;} imageView.setImageResource (images [position]) Return imageView;}}

MainActivity.java

Package com.example.demo0330; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.GridView;import android.widget.Toast; public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); GridView gridView= (GridView) findViewById (R.id.gr1); gridView.setAdapter (new ImageAdapter (this)) GridView.setOnItemClickListener (new AdapterView.OnItemClickListener () {@ Override public void onItemClick (AdapterView adapterView, View view, int position, long l) {Toast.makeText (MainActivity.this, "now click on the location" + (position+1) + "photo", Toast.LENGTH_LONG). Show ();}});}}

Effect picture:

This is the end of this article on "how to use Android Spinner and GridView components". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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