目录:
1. 概述
2. AdapterView编程模式介绍
3. ListView组件使用示例
4. Adapter接口及其常用方法
5. 总结
1. 概述
在Android应用开发中,AdapterView是一类常用且非常重要的组件。我们常见的以列表的形式显示信息的组件就是AdapterView的子类,称为Listview;我们经常以网格方式浏览图片缩略图的组件也是AdapterView的子类,被称为GridView;以下拉列表形式显示可选项的组件也是AdapterView的子类,称为Spinner;还有等等它们都是AdapterView的子类。
2. AdapterView编程模式介绍
AdapterView提供了一种灵活的方式来管理和显示数据。通过使用不同的子类(如ListView、GridView、Spinner等),可以实现各种不同形式的列表展示。要使用AdapterView显示信息,需要完成以下几个步骤:
- 在界面布局中包含一个AdapterView组件;
- 对在列表中显示的列表项进行布局;
- 设计一个实现了Adapter接口的类,用于为AdapterView组件提供需要显示的数据。
3. ListView组件使用示例
以Android的ListView组件为例,以列表形式显示Android系统中已经安装的所有程序信息。当显示的内容超出物理屏幕可用区域时,它还可以进行滚动,就跟ScrollView的效果一样。
示例代码:
```java
// 在界面布局中包含一个ListView组件
android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` ```java // 对在列表中显示的列表项进行布局 ArrayAdapter ListView listView = findViewById(R.id.list_view); listView.setAdapter(adapter); ``` ```java // 设计一个实现了Adapter接口的类,用于为ListView组件提供需要显示的数据 public class InstalledAppsAdapter extends ArrayAdapter private Context context; private List private int resource; int selectedPosition = -1; // 默认选中第一个应用 Drawable icon; // 应用图标资源ID Drawable selectedIcon; // 选中状态的应用图标资源ID TextView appName; // 应用名称文本视图资源ID TextView appLabel; // 应用标签文本视图资源ID TextView appSize; // 应用大小文本视图资源ID TextView appPath; // 应用安装路径文本视图资源ID TextView appVersion; // 应用版本文本视图资源ID TextView appInstallDate; // 应用安装日期文本视图资源ID TextView appLastUpdateDate; // 应用最后更新日期文本视图资源ID TextView appRating; // 应用评分文本视图资源ID TextView appPackageName; // 应用包名文本视图资源ID TextView appDownloads; // 应用下载次数文本视图资源ID TextView appShared; // 应用共享次数文本视图资源ID TextView appOpenSource; // 应用开源次数文本视图资源ID TextView appLastUsedDate; // 应用最后使用日期文本视图资源ID TextView appMimeType; // 应用MIME类型文本视图资源ID TextView appFlags; // 应用标志文本视图资源ID TextView appPrivateFlags; // 应用私有标志文本视图资源ID TextView appProtectedFlags; // 应用受保护标志文本视图资源ID TextView appExternalNativeLibDirs; // 应用外部本地库目录文本视图资源ID TextView appMediapath; // 应用媒体路径文本视图资源ID TextView appHasCodesigningKey; // 应用是否具有代码签名密钥文本视图资源ID TextView appHasPermanentStorage; // 应用是否具有永久存储空间文本视图资源ID TextView appHasHardwareAcceleration; // 应用是否具有硬件加速功能文本视图资源ID TextView appIsDebuggable; // 应用是否可调试文本视图资源ID TextView appMinorVersion; // 应用次要版本号文本视图资源ID TextView appMajorVersion; // 应用主要版本号文本视图资源ID TextView appTargetSdkVersion; // 应用目标SDK版本号文本视图资源ID TextView appMaxSdkVersion; // 应用最大SDK版本号文本视图资源ID TextView appMinSdkVersion; // 应用最小SDK版本号文本视图资源ID TextView appBuildNumber; // 应用构建号文本视图资源ID TextView appVersionCode; // 应用版本代码文本视图资源ID TextView appReleaseDate; // 应用发布日期文本视图Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES.LOLLIPOP) or release date text view Resource ID (API < Build.VERSION_CODES.LOLLIPOP) Text View Resource ID (API >= Build.VERSION_CODES 以下是根据您提供的内容重构的代码: ```java public class MainActivity extends AppCompatActivity { private ListView mListView; private ArrayAdapter private String[] mData = {"item1", "item2", "item3", "item4", "item5"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.lv1); mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mData); mListView.setAdapter(mAdapter); } } ``` activity_main.xml: ```xml android:id="@+id/lv1" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` item.xml: 由于您没有提供item.xml文件,我无法为您提供具体的内容。但是,您可以根据需要创建一个简单的列表项布局文件,如下所示: ```xml android:id="@+id/tv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" /> ``` ```java import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class MyAdapter extends RecyclerView.Adapter private Context context; public MyAdapter(Context context) { this.context = context; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); return new ViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { // TODO: Add your logic to bind data to the view holder } @Override public int getItemCount() { // TODO: Return the total number of items in the data set return 0; } public static class ViewHolder extends RecyclerView.ViewHolder { // TODO: Define your views here public ViewHolder(@NonNull View itemView) { super(itemView); // TODO: Find and assign your views to their corresponding IDs or variables } } } ``` 以下是重构后的内容: ```java package 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 MyAdapter extends BaseAdapter { private String[] books = {"Java程序设计", "Android应用开发", "oracle数据库管理指南", "JavaWeb程序设计", "软件工程之系统工程师之路"}; LayoutInflater inflater; int id_item; public MyAdapter(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.java package 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); myAdapter myAdapter = new myAdapter(this, R.layout.item); lv.setAdapter(myAdapter); } } ``` 改进后的activity_main.xml: ``` xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> android:id="@+id/lv1" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 这是一个名为"item.xml"的文件,它位于RelativeLayout的ListView中。该文件包含一个ListView元素,其ID为"@+id/lv1",宽度填充父级容器,高度根据内容自适应(wrap_content)。 以下是重构后的代码: ```xml xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:stretchColumns="2"> android:id="@+id/lv1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> ``` ```java import android.os.Bundle; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MyAdapter extends RecyclerView.Adapter private String[] bookNames; private int[] bookPhones; public MyAdapter(String[] bookNames, int[] bookPhones) { this.bookNames = bookNames; this.bookPhones = bookPhones; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.book_item, parent, false); return new ViewHolder(itemView); } @Override public void onBindViewHolder(ViewHolder holder, int position) { ImageView bookPhone = holder.bookPhone; TextView bookName = holder.bookName; int phone = bookPhones[position]; String name = bookNames[position]; bookPhone.setImageResource(phone); bookName.setText(name); } @Override public int getItemCount() { return bookNames.length; } public static class ViewHolder extends RecyclerView.ViewHolder { ImageView bookPhone; TextView bookName; public ViewHolder(View itemView) { super(itemView); bookPhone = itemView.findViewById(R.id.book_phone); bookName = itemView.findViewById(R.id.book_name); } } } ``` 下面是根据您提供的代码内容,重构后的代码段: ```java package 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 MyAdapter extends BaseAdapter { private BookItem[] books = { new BookItem("陈某人", R.drawable.dog), new BookItem("周某人", R.drawable.dog), new BookItem("钟某人", R.drawable.dog), new BookItem("林某人", R.drawable.dog), new BookItem("涛某人", R.drawable.dog) }; LayoutInflater inflater; int id_item; public MyAdapter(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 convertView, ViewGroup parent) { LinearLayout linearLayout = (LinearLayout) inflater.inflate(id_item, parent, false); ImageView imageView = (ImageView) linearLayout.findViewById(R.id.book_phone); imageView.setImageResource(books[position].photo); TextView textView = (TextView) linearLayout.findViewById(R.id.book_name); textView.setText(books[position].name); return linearLayout; } /** * 定义一个图片类 */ private class BookItem { String name; int photo; public BookItem(String name, int photo) { this.name = name; this.photo = photo; } } } ``` ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } ``` ```java package 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 = "确定选择" + name + "今晚打火锅吗"; Toast.makeText(this, text, Toast.LENGTH_LONG).show(); } } ``` 很抱歉,我不太明白您的意思。您能否再详细说明一下您的需求呢?😊