ArrayAdapter列表适配器SimpleAdapter简单

2018-05-18  本文已影响0人  三度_f8ac

ArrayAdapter列表适配器SimpleAdapter简单的适配器BaseAdapter基础适配器package com.example.layou_text; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.string; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class CopyOfdi1Activity extends Activity { ListView lv_man; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lv_man);//加载(是布局不是id!) lv_man=(ListView)findViewById(R.id.lv_man);//获取操作 // 注意写法不能写R.layout.获取布局 //准备数据 String [] data={"a","b","c","d","e","f","g","h"}; //构造arrayaadapter适配(上下文,加载另一个布局id,传入数据(数据会被传入布局中)) ArrayAdapteradapter = new ArrayAdapter(CopyOfdi1Activity.this, R.layout.item_array_adapter,data); // 加载textview体系的单个布局 //给lv_man设置适配器 lv_man.setAdapter(adapter); }}xml:(定义一个listview布局)id+lv_man.xml?xml version="1.0" encoding="utf-8"? 再写一个xml(定义listView里面的内容textview)?xml version="1.0" encoding="utf-8"? package com.example.layou_text; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.string; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class CopyOfdi2Activity extends Activity { ListView lv_man; Listdata; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lv_man);//加载(是布局不是id!) lv_man=(ListView) findViewById(R.id.lv_man);//获取操作 ArrayList> arrayList = new ArrayList>(); //new了一个Arraylist数组包含map集合 HashMaphashMap = new HashMap(); //new了一个map集合编写key value hashMap=new HashMap(); //简写用上面hashMap的对象再新建一个map集合,赋值,添加到arrayList集合中 hashMap.put("icom", R.drawable.f2); hashMap.put("name","美食--2"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f3); hashMap.put("name","美食--3"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f4); hashMap.put("name","美食--4"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f5); hashMap.put("name","美食--5"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f6); hashMap.put("name","美食--6"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f7); hashMap.put("name","美食--7"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f8); hashMap.put("name","美食--8"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f9); hashMap.put("name","美食--9"); hashMap.put("content","内容"); arrayList.add(hashMap); hashMap=new HashMap(); hashMap.put("icom", R.drawable.f10); hashMap.put("name","美食--10"); hashMap.put("content","内容"); arrayList.add(hashMap); //arrayList集合里面有许多个map集合,map集合中有许多的数据 // Map对象中的key的数组,用于得到对应的value String from[]={"icom","name","content"};//定义from代表map里面的key值 // item布局文件中的子View的id的数组 int [] to ={R.id.imageView1,R.id.tv_item_name,R.id.tv_item_content,}; //定义to代表布局控件 // 准备SimpleA SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList, R.layout.activity_chatn, from, to); //加载包含to的控件视图(不是视图id),from是通过key获取了value值, // to是把value赋给了布局中的控件 lv_man.setAdapter(simpleAdapter); // 设置适配器 } }lv_man.xml布局写?xml version="1.0" encoding="utf-8"? 再写一个xml(定义listView里面的内容控件) package com.example.layou_text; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.string; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class MainActivity extends Activity { ListView lv_man;//列表视图 Listdata;//不用map用了shoinfo类来储存数据集合 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lv_man);//加载(是布局不是id!) lv_man=(ListView) findViewById(R.id.lv_man);//获取操作 // 准备集合数据 data =new ArrayList(); data.add(new shoinfo(R.drawable.f1,"美食名--1","美食内容--1")); data.add(new shoinfo(R.drawable.f2,"美食名--2","美食内容--2")); data.add(new shoinfo(R.drawable.f3,"美食名--3","美食内容--3")); data.add(new shoinfo(R.drawable.f4,"美食名--4","美食内容--4")); data.add(new shoinfo(R.drawable.f5,"美食名--5","美食内容--5")); data.add(new shoinfo(R.drawable.f6,"美食名--6","美食内容--6")); data.add(new shoinfo(R.drawable.f7,"美食名--7","美食内容--7")); data.add(new shoinfo(R.drawable.f8,"美食名--8","美食内容--8")); data.add(new shoinfo(R.drawable.f9,"美食名--9","美食内容--9")); data.add(new shoinfo(R.drawable.f10,"美食名--10","美食内容--10")); // 准备Baseadapter对象 Myadapter myadapter = new Myadapter(); //设置Adapter显示列表 lv_man.setAdapter(myadapter); } class Myadapter extends BaseAdapter{//定义一个局部内部类继承BaseAdapter //返回集合的总数 @Override public int getCount() { return data.size(); } //返回指定下标对应的数据对象 @Override public Object getItem(int position) { return data.get(position); } //返回每个条目的ID @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } //返回指定下标所对应的Item的View对象(试图和总数) // position下标 // convertView可以复用的缓存Item视图对象, 前n+1个为null // parent listView对象 @Override public View getView(int position, View convertView, ViewGroup parent) { // 1.创建或得到对应当前行的一个Viewholder对象 ViewHolder hole=null; if(convertView==null){ // 加载布局Item的布局,得到View对象 convertView = View.inflate(MainActivity.this, R.layout.activity_chatn, null); hole=new ViewHolder(); // 获取到convertView的子view的控件 hole.findViewById = (ImageView) convertView.findViewById(R.id.imageView1); hole.view = (TextView) convertView.findViewById(R.id.tv_item_name); hole.view1 = (TextView) convertView.findViewById(R.id.tv_item_content); // 将holder对象保存到converterView上 convertView.setTag(hole); }else{ hole=(ViewHolder)convertView.getTag(); } // 根据position设置对应的数据 // 得到当行的数据对象 shoinfo shoinfo = data.get(position); // 得到子View对象 // 给ViewHolder对象的视图设置数据 hole.findViewById.setImageResource(shoinfo.getIncon()); hole.view.setText(shoinfo.getName()); hole.view1.setText(shoinfo.getContent()); return convertView; } //数据类 class ViewHolder{ public ImageView findViewById; public TextView view; public TextView view1; } } }java写一个存放数据类package com.example.layou_text; public class shoinfo { public int incon; public String name; public String content; @Override public String toString() { return "shoinfo [incon=" + incon + ", name=" + name + ", content=" + content + "]"; } public int getIncon() { return incon; } public void setIncon(int incon) { this.incon = incon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public shoinfo(int incon, String name, String content) { super(); this.incon = incon; this.name = name; this.content = content; } }定义一个listview(id+lv_man) 再写一个xml(定义listView里面的内容控件)

上一篇下一篇

猜你喜欢

热点阅读