列表和适配器(ListView和Adapter)
2016-04-06 00:24
295 查看
列表
布局文件里写:
value-strings里面添加
2.适配器
连接后端数据和前端显示的接口
决定数据在UI上如何显示
BaseAdapter:抽象类,具有较高的灵活性
(先新建一个News实体类)
新建一个package名为adapter,在下面新建一个java文件MyAdapter
新建一个Activity命名为BaseAdapterActivity
设置监听可直接在MyAdapter里面写匿名监听
布局文件里写:
<?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" tools:context="com.example.ygd.jreduch04.ListViewActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list1" android:entries="@array/listData" > </ListView> </RelativeLayout>
value-strings里面添加
<string-array name="listData"> <item>张三</item> <item>李四</item> <item>王五</item> <item>赵六</item> <item>田七</item> </string-array>
2.适配器
连接后端数据和前端显示的接口
决定数据在UI上如何显示
ArrayAdapter:最为简单,只能展示一行文字 //ListView本身有与ArrayAdapter对应的接口 ListView lv=(ListView)findViewById(R.id.list1); //定义ListView要显示的数据 String[] names={"张三","李四","王五","赵六","田七","胡巴","久久"}; /*初始化适配器 参数1:Context上下文信息 参数2:布局文件 参数3:要填充的数据 */ ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,names); //ListView设置适配器 lv.setAdapter(aa); Spinner用法与其一致: //定义ListView要显示的数据 String[] names={"张三","李四","王五","赵六","田七","胡巴","久久"}; /*初始化适配器 参数1:Context上下文信息 参数2:布局文件 参数3:要填充的数据 */ ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_spinner_item,names); Spinner sp=(Spinner)findViewById(R.id.sp1); sp.setAdapter(aa); SimpleAdapter有较好的扩充性,可以自定义布局 (from指的是来自map的哪一组数据,to指的是放到哪一个控件里去) List list=new ArrayList(); Map map=new HashMap(); map.put("name", "张三"); list.add(map); map=new HashMap(); map.put("name", "李四"); list.add(map); map=new HashMap(); map.put("name", "王五"); list.add(map); map=new HashMap(); map.put("name", "赵六"); list.add(map); SimpleAdapter sa=new SimpleAdapter(this,list,android.R.layout.simple_list_item_1,new String[]{"name"},new int[]{android.R.id.text1}); ListView lv=(ListView)findViewById(R.id.list1); lv.setAdapter(sa); simple_list_item_2的用法: List list=new ArrayList(); Map map=new HashMap(); map.put("name", "张三"); map.put("age",20); list.add(map); map=new HashMap(); map.put("name", "李四"); map.put("age",21); list.add(map); map=new HashMap(); map.put("name", "王五"); map.put("age",22); list.add(map); map=new HashMap(); map.put("name", "赵六"); map.put("age",23); list.add(map); SimpleAdapter sa=new SimpleAdapter(this,list,android.R.layout.simple_list_item_2,new String[]{"name","age"},new int[]{android.R.id.text1,android.R.id.text2}); ListView lv=(ListView)findViewById(R.id.list1); lv.setAdapter(sa); //设置监听 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map m=(Map)list.get(position); Toast.makeText(getBaseContext(),"您点击了"+(m.get("name")),Toast.LENGTH_SHORT).show(); } });
BaseAdapter:抽象类,具有较高的灵活性
(先新建一个News实体类)
package com.example.ygd.jreduch04.entity; /** * Created by ygd on 2016/4/5. */ public class News { private int newsId; private String title; private String content; private String pubDate; private String newsFrom; private int[] img; private String comCount; public News(){} public News(int newsId, String title, String content, String pubDate, String newsFrom, int[] img, String comCount) { this.newsId = newsId; this.title = title; this.content = content; this.pubDate = pubDate; this.newsFrom = newsFrom; this.img = img; this.comCount = comCount; } public int getNewsId() { return newsId; } public void setNewsId(int newsId) { this.newsId = newsId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getPubDate() { return pubDate; } public void setPubDate(String pubDate) { this.pubDate = pubDate; } public String getNewsFrom() { return newsFrom; } public void setNewsFrom(String newsFrom) { this.newsFrom = newsFrom; } public int[] getImg() { return img; } public void setImg(int[] img) { this.img = img; } public String getComCount() { return comCount; } public void setComCount(String comCount) { this.comCount = comCount; } }
新建一个package名为adapter,在下面新建一个java文件MyAdapter
package com.example.ygd.jreduch04.adapter; 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.TextView; import com.example.ygd.jreduch04.R; import com.example.ygd.jreduch04.entity.News; import java.util.List; public class MyAdapter extends BaseAdapter{ private List<News> myData; private Context context; public MyAdapter(Context context,List myData) { this.context = context; this.myData = myData; } @Override public int getCount() { return myData.size(); } @Override public Object getItem(int position) { return myData.get(position); } @Override public long getItemId(int position) { return myData.get(position).getNewsId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v=LayoutInflater.from(context).inflate(R.layout.list_layout_1,null); ImageView imageView=(ImageView)v.findViewById(R.id.imag1); TextView textView=(TextView)v.findViewById(R.id.text1); News news=myData.get(position); textView.setText(news.getTitle()); imageView.setImageResource(news.getImg()[0]); return v; } }
新建一个Activity命名为BaseAdapterActivity
package com.example.ygd.jreduch04; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import com.example.ygd.jreduch04.adapter.MyAdapter; import com.example.ygd.jreduch04.entity.News; import java.util.ArrayList; import java.util.List; public class BaseAdapterActivity extends AppCompatActivity { private List myData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_base_adapter); loadData(); ListView listView=(ListView)findViewById(R.id.list1); MyAdapter ma=new MyAdapter(this,myData); listView.setAdapter(ma); } public void loadData(){ myData=new ArrayList(); News news=new News(1,"亚冠1","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.ic_launcher},"800"); myData.add(news); news=new News(2,"亚冠2","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.aa},"800"); myData.add(news); news=new News(3,"亚冠3","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.bb},"800"); myData.add(news); news=new News(4,"亚冠4","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.dice01},"800"); myData.add(news); news=new News(5,"亚冠5","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.dice02},"800"); myData.add(news); news=new News(6,"亚冠6","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.dice03},"800"); myData.add(news); news=new News(7,"亚冠7","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.dice04},"800"); myData.add(news); news=new News(8,"亚冠8","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.dice05},"800"); myData.add(news); news=new News(9,"亚冠9","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.dice06},"800"); myData.add(news); news=new News(10,"亚冠10","首尔FCvs山东鲁能","2016-04-05","新浪体育",new int[]{R.mipmap.ic_launcher},"800"); myData.add(news); } }
设置监听可直接在MyAdapter里面写匿名监听
相关文章推荐
- Gradle 修改 Maven 仓库地址(转)
- xml解析之SAXParser的简单使用
- Spring Data Redis—Pub/Sub(附Web项目源码) (转)
- java-lang-OutOfMemoryError-PermGen-space
- 初探nodeJS
- 在VPS上部署javaweb应用
- 关于汉诺塔问题
- 自定义标题栏
- -Dmaven.multiModuleProjectDirectory system propery is not set.
- TextView设置文本部分可点击时出现文本不显示的问题分析与解决
- java原生的zip压缩与解压缩实现
- 利用maven中resources插件的copy-resources目标进行资源copy和过滤
- SIGURG信号 -- 带外数据的处理
- Intellij Maven创建SpringMVC 框架
- Java synchronized详解
- AtomicInteger
- OSChina 周三乱弹 ——吸血鬼哪有这么色!
- 主要电商网站首屏性能对比
- 深入理解Dalvik虚拟机- 解释器的运行机制
- ubuntu下安装使用vmware、kvm、xen