Android-Adapter实现方法(ArrayAdapter,SimpleAdapter,SimpleCursorAdapter)
来源:互联网 发布:三级域名申请 编辑:程序博客网 时间:2024/05/18 05:21
Adapter
总结
Adapter常用的实现类包括ArrayAdapter、SimpleAdapter、SimpleCursorAdapter、BaseAdapter。Adapter可以实现数据先加载再动态显示在界面上,而不是固定死界面有多少个列表(类似于web开发中的动态网页),是一个非常重要的内容。
Adapter 常用实现类:
ArrayAdapter(基于ListActivity)
使用简单,但是只能实现文本内容(TextView)
package com.example.huaro.proj_09;import android.app.ListActivity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ArrayAdapter;public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] arr=new String[]{ "猪八戒", "何洪达", "萌萌哒" }; ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,arr); setListAdapter(arrayAdapter); }}
SimpleAdapter (图片和文字)
并不简单,功能强大,大部分的列表实现均可使用SimpleAdapter
<!--main界面代码,定义一个ListView--><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
<!--simple_item界面代码 嵌入main界面中的ListView--><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"><ImageView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:id="@+id/desc"/> </LinearLayout></LinearLayout>
//Activity代码package com.example.huaro.proj_09;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by HUARO on 2016/2/10 0010. */public class Test extends Activity{ //位于/res/drawable下的三张图片 private int[] images=new int[]{ R.drawable.img_08, R.drawable.img_08, R.drawable.img_08 }; //数据 private String[] names=new String[]{ "你好", "萌萌哒", "呵呵大" }; //数据 private String[] descs=new String[]{ "我就是我不一样的烟火", "你就是你", "哈哈" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //加载界面 setContentView(R.layout.main); //定义List集合 List<Map<String,Object>> listmap=new ArrayList<Map<String,Object>>(); //List集合添加数据 for (int i=0;i<names.length;i++){ Map<String,Object> listItem=new HashMap<String,Object>(); listItem.put("header",images[i]); listItem.put("personName",names[i]); listItem.put("desc",descs[i]); listmap.add(listItem); } /* *创建一个simpleListView *参数(context,List,view,String[]{"list中的各项参数"},int[]{"对应显示界面的id"}) */ SimpleAdapter simpleAdapter=new SimpleAdapter(this,listmap,R.layout.simple_item,new String[]{"personName","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc}); //获取界面ListView ListView list=(ListView)findViewById(R.id.mylist); //为ListView设置Adapter list.setAdapter(simpleAdapter); //为ListView的列表项绑定点击事件 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Toast提示消息 Toast.makeText(MainActivity.this, "数据"+names[position]+":"+descs[position], Toast.LENGTH_SHORT).show(); } }); list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //Toast提示消息 Toast.makeText(MainActivity.this, "数据"+names[position]+":"+descs[position], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); }}
SimpleCursorAdapter(SQLite数据库)
<!--两个文本框,一个按钮,用作数据插入数据库--><?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.huaro.proj_data_sqlite.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/edit1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/edit2" android:layout_below="@+id/edit1" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="插入" android:id="@+id/button" android:layout_below="@+id/edit2" android:layout_centerHorizontal="true" android:layout_marginTop="45dp" android:onClick="insertSQL"/> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/show" android:layout_below="@+id/button" android:layout_centerHorizontal="true" /></RelativeLayout>
<!--用作显示数据库内容--><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/my_title" android:layout_weight="0.03" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/my_content" android:layout_weight="0.06" /></LinearLayout>
package com.example.huaro.proj_data_sqlite;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CursorAdapter;import android.widget.EditText;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class MainActivity extends AppCompatActivity { SQLiteDatabase sqLiteDatabase; ListView listView; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sqLiteDatabase=SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"my.db3",null); listView=(ListView)findViewById(R.id.show); } public void insertSQL(View view){ String title=((EditText)findViewById(R.id.edit1)).getText().toString(); String content=((EditText)findViewById(R.id.edit2)).getText().toString(); try{ insertData(sqLiteDatabase,title,content); Cursor cursor=sqLiteDatabase.rawQuery("select *from news_inf",null); inflateList(cursor); }catch (Exception e){ //执行ddl创建数据表 sqLiteDatabase.execSQL("create table news_inf(_id integer"+" primary key autoincrement,"+" news_title varchar(50),"+" news_content varchar(255))"); //执行insert语句插入数据 insertData(sqLiteDatabase,title,content); //执行查询 Cursor cursor=sqLiteDatabase.rawQuery("select *from news_inf",null); inflateList(cursor); } } private void insertData(SQLiteDatabase db,String title,String content){ //执行插入语句 db.execSQL("insert into news_inf values(null,?,?)",new String[]{title,content}); } private void inflateList(Cursor cursor){ //填充SimpleCursorAdapter SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(MainActivity.this,R.layout.line,cursor,new String[]{"news_title","news_content"},new int[]{R.id.my_title,R.id.my_content}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); //显示数据 listView.setAdapter(simpleCursorAdapter); } @Override protected void onDestroy() { super.onDestroy(); //退出程序时关闭SQLiteDatabase if(sqLiteDatabase!=null&&sqLiteDatabase.isOpen()){ sqLiteDatabase.close(); } }}
do a small fish in the sea;
0 0
- Android-Adapter实现方法(ArrayAdapter,SimpleAdapter,SimpleCursorAdapter)
- Android学习之Adapter:SimpleAdapter,SimpleCursorAdapter,ArrayAdapter(数据库 列表)
- [Android] Adapter:SimpleAdapter SimpleCursorAdapter ArrayAdapter 与ListView的用法
- [Android]ArrayAdapter ,SimpleAdapter ,SimpleCursorAdapter 区别
- Android SimpleAdapter,SimpleCursorAdapter,ArrayAdapter的区别
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- ArrayAdapter ,SimpleAdapter ,SimpleCursorAdapter 区别
- ArrayAdapter ,SimpleAdapter ,SimpleCursorAdapter 区别
- ArrayAdapter SimpleAdapter SimpleCursorAdapter 区别
- ArrayAdapter ,SimpleAdapter ,SimpleCursorAdapter 区别
- BaseAdapter ArrayAdapter SimpleCursorAdapter SimpleAdapter BaseExpandableListAdapter
- ListView适配器--ArrayAdapter;SimpleAdapter;SimpleCursorAdapter
- ArrayAdapter ,SimpleAdapter与SimpleCursorAdapter 区别
- Android之ListView使用方法-ArrayAdapter-SimpleCursorAdapter-SimpleAdapter-BaseAdapter
- 【Android学习笔记】Adapter - ArrayAdapter、SimpleAdapter
- Android Adapter接口及实现类之ArrayAdapter、SimpleAdapter和BaseAdapter的简单介绍及运用
- POJ 3481 Double Queue(STL)
- linux(ubuntu) 创建一个名为hadoop的用户和用户组
- python for循环语句怎么写
- 关于前端程序员写前端用什么框架更好?
- VLC Android播放器介绍
- Android-Adapter实现方法(ArrayAdapter,SimpleAdapter,SimpleCursorAdapter)
- leetcode-94-Binary Tree Inorder Traversal
- lintcode:Remove Node in Binary Search Tree
- 利用php的explode函数将字符串按分隔符(比如空格)分拆并组装在数组中-----要考虑连续空格问题
- UVA 442(p141)----Matrix Chain Multiplication
- HDU 2222 Keyword Search(AC自动机)
- Cpp_类型转换
- HDMI和SDI的简介
- codeforces 589H DFS