当前位置:网站首页>ArrayAdapter(数组适配器)与SimpleAdapter(简单适配器)
ArrayAdapter(数组适配器)与SimpleAdapter(简单适配器)
2022-07-27 17:02:00 【Ashurol】
ArrayAdapter数组适配器常用语搭建简单的数组列表等布局,SimpleAdapter简单适配器的布局文件可以自己定义。
public class MainActivity extends ActionBarActivity implements OnItemClickListener,OnScrollListener{
ListView listView1;
List<Map<String,Object>> datalist;
ArrayAdapter<String> Adapter;
SimpleAdapter Adapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1=(ListView) findViewById(R.id.listView1);
//创建数组适配器的数据源
//创建简单适配器数据源
String list[]={"第一行","第二行","第三行","第四行"};
datalist=new ArrayList<Map<String,Object>>();
//创建数组适配器
//创建简单适配器
/*
* 数组适配器里的三个参数分别是上下文,简单布局文件,数据源。
* 简单适配器参数分别为上下文,数据源(一般都是泛型集合),自己定义的布局文件,for是数据源键值对的名称,to是与for相对应的ID地址
*/
Adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
Adapter2=new SimpleAdapter(this, getdata(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.img,R.id.tv});
//为listView1绑定适配器
//listView1.setAdapter(Adapter);
listView1.setAdapter(Adapter2);
//注册监听器
listView1.setOnItemClickListener(this);
listView1.setOnScrollListener(this);
}
//为简单适配器数据源添加内容
private List<Map<String,Object>> getdata()
{
for(int i=0;i<15;i++)
{
Map<String,Object> map=new HashMap<String,Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "第"+i+"行");
datalist.add(map);
}
return datalist;
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView arg0, int ScrollState) {
// TODO Auto-generated method stub
//ScrollState监测下拉状态
switch(ScrollState)
{
case SCROLL_STATE_FLING://下拉形成惯性继续滑动
//为视图增加一个条目
Map<String,Object> map=new HashMap<String,Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "新增条目");
datalist.add(map);
//刷新视图
Adapter2.notifyDataSetChanged();
break;
case SCROLL_STATE_IDLE://视图已经停止滑动
break;
case SCROLL_STATE_TOUCH_SCROLL://下拉中,手指没离开屏幕
break;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
//获取点击项内容
String text=listView1.getItemAtPosition(position)+"";
Toast.makeText(MainActivity.this,"position:"+position+"text:"+text,Toast.LENGTH_SHORT).show();
}
}自定义的布局文件Item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/tv"
android:text="nihao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>数组适配器的视图 
简单适配器

添加OnItemClickListener监听器操作

添加OnScrollListener操作

边栏推荐
- To create a MySQL data source resource group, you must choose to create a new exclusive data integration resource group? Or use a common resource group? thank you
- ReferenceError: __dirname is not defined in ES module scope
- 嵌入式C语言结构体
- A low code development platform that brings high-value user experience
- [basic knowledge of deep learning - 47] Bayesian networks and naive Bayes
- Complex number proof of solvability of regular 17 sided ruler and gauge drawing
- [daily accumulation - 06] view CUDA and cudnn versions
- [deep learning target detection series - 01] what is target detection
- c语言:10、输入流,输出流,错误流
- 时间复杂度和空间复杂度
猜你喜欢

【深度学习基础知识 - 46】贝叶斯定理与条件概率公式

Complete source code of E-commerce mall applet project (wechat applet)

C language: 11. Pipeline
技术实践干货 | 初探大规模 GBDT 训练

C language: 14. Preprocessing

C language: 10. Input stream, output stream, error stream

Map and set

C language: 15. Structure

Basic network faults and troubleshooting

Tab control of MFC advanced control (CTabCtrl)
随机推荐
英特尔未来10年工艺路线图曝光:2029年推出1.4nm工艺!如何实现?
27. Basics of golang - mutex lock, read / write lock
IEC104 规约详细解读(一) 协议结构
Map and set
Time complexity and space complexity
ReferenceError: __ dirname is not defined in ES module scope
【深度学习基础知识 - 38】L1正则化和L2正则化的区别
A lock faster than read-write lock. Don't get to know it quickly
C language: 7. How to use C language multi source files
Optimization of fixed number of cycles in embedded C language
开启和禁用hyper-v
C language: 8. Makefile preparation
细数国产接口协作平台的六把武器!
influxDB系列(三)influxDB配置文件详解
Binary search tree
二叉搜索树
几种无线协议简介
c语言:5、多维数组
27、golang基础-互斥锁、读写锁
HDU1573 X问题【一元线性同余方程组】