ArrayAdapter,SimpleAdapter的基本使用
来源:互联网 发布:文网文添加域名 编辑:程序博客网 时间:2024/06/05 12:47
一.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="listview示例" /> <RadioGroup android:id="@+id/rg_radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="适配器模式" > <RadioButton android:id="@+id/rb_arrayadapter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ArrayAdapter" /> <RadioButton android:id="@+id/rb_simpleadapter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SimpleAdapter" /> <RadioButton android:id="@+id/rb_baseadapter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BaseAdapter" /> </RadioGroup> <ListView android:id="@+id/lv_listviewdemo" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/></LinearLayout>
1.ArrayAdapter子布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" android:id="@+id/tv_arrayadapter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ceshi" /> <RadioButton android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
2.SimpleAdapter子布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="nihao" /> <TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="niffo" /></RelativeLayout>
二。java文件
package com.m520it.listviewreview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.SimpleAdapter;import android.widget.TextView;public class MainActivity extends Activity { private RadioButton arrRadio; private RadioButton baseRadio; private RadioButton simpleRadio; private RadioGroup meduleradiogroup; private ListView lvdemo; private TextView titletv; private ArrayAdapter myArrayAdapter; private SimpleAdapter mSimpleAdapter; private List<String> adapterone; private ArrayList<Map<String, String>> simpledata; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initArrayData(); initSimpleData(); initView(); } public void initArrayData(){// myArrayAdapter// myBaseAdapter// mSimpleAdapter adapterone = new ArrayList<String>(); for(int i = 0 ; i < 100 ; i++){ String itemResult = "arrayitem"+i; adapterone.add(itemResult); } } public void initSimpleData() { simpledata = new ArrayList<Map<String,String>>(); for(int i = 0;i<100;i++){ Map<String, String> simpleMap = new HashMap<String, String>(); simpleMap.put("name","小"+i); simpleMap.put("age", "年龄"+i); simpledata.add(simpleMap); } } public void initView(){ setContentView(R.layout.activity_main); arrRadio = (RadioButton) findViewById(R.id.rb_arrayadapter); baseRadio = (RadioButton) findViewById(R.id.rb_baseadapter); simpleRadio = (RadioButton) findViewById(R.id.rb_simpleadapter); meduleradiogroup = (RadioGroup) findViewById(R.id.rg_radiogroup); lvdemo = (ListView)findViewById(R.id.lv_listviewdemo); titletv = (TextView)findViewById(R.id.tv_title); meduleradiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_arrayadapter: titletv.setText("array"); if(myArrayAdapter == null){ myArrayAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.listdemo_item,R.id.tv_arrayadapter,adapterone); } lvdemo.setAdapter(myArrayAdapter); break; case R.id.rb_baseadapter: titletv.setText("base"); break; case R.id.rb_simpleadapter: titletv.setText("simple"); if(mSimpleAdapter == null){ mSimpleAdapter = new SimpleAdapter(MainActivity.this,simpledata,R.layout.simple_item,new String[]{"name","age"},new int[]{R.id.tv_name,R.id.tv_age}); } lvdemo.setAdapter(mSimpleAdapter); break; } } }); }}
0 0
- ArrayAdapter,SimpleAdapter的基本使用
- ArrayAdapter, SimpleAdapter 和BaseAdapter的使用范例
- ListView的使用----ArrayAdapter与SimpleAdapter
- 20.ArrayAdapter和SimpleAdapter的使用
- 使用ArrayAdapter和SimpleAdapter
- SimpleAdapter的基本使用
- ArrayAdapter和SimpleAdapter简单使用
- SimpleAdapter 和 ArrayAdapter使用详解
- ArrayAdapter适配器和SimpleAdapter适配器以及ListView的使用
- Android实例demo11之listview的简单使用(arrayadapter、simpleadapter)
- 3.2列表的使用listview与BaseAdapter、simpleAdapter、ArrayAdapter
- ListView和ArrayAdapter、SimpleAdapter适配器的简单使用
- Android 中ArrayAdapter,SimpleAdapter,BaseAdapter三种适配器的使用
- Android 中ArrayAdapter,SimpleAdapter,BaseAdapter三种适配器的使用
- 安卓开发(4):ListView的使用,arrayAdapter,simpleAdapter
- Android SimpleAdapter,SimpleCursorAdapter,ArrayAdapter的区别
- 我的Android进阶之旅------>Android ListView 应用解析(使用ArrayAdapter,SimpleAdapter和SimpleCursorAdapter适配器)
- Android中ListView常用的两种适配器ArrayAdapter和SimpleAdapter使用
- 使用fresco遇到的坑-SimpleDraweeView
- 上传本地代码到github上
- 数据结构实验之栈四:括号匹配
- Vue.js——60分钟browserify项目模板快速入门【7】
- GraphicsLab Project之Color Processing
- ArrayAdapter,SimpleAdapter的基本使用
- Sicily 1001.Alphacode | 动态规划
- python堆排序
- 暑期社会实践心得
- java字符串分解 StringTokenizer用法
- java各大排序算法总结
- Vue.js——60分钟webpack项目模板快速入门【8】
- 二叉树的非递归遍历
- 《自控力》阅读笔记二