关于listview的自定义适配器
listview是什么?
listview是展示列表的view
listview的作用?
展示列表
适配器的作用是什么?
适配器是将数据显示在listview上的中介
数据是什么?
具体的将被映射的字符串,图片,或者基本组件。
listview自定义适配器的步骤?
第一步:创建listview xml文件 并找到id;
第二步:创建一个装数据的容器 此处我用的是arraylist;
ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String, String>>
第三步:将数据放入容器中:
hash = new HashMap<String, String>();
hash.put("ctime", ctime);
hash.put("title", title);
hash.put("description", description);
hash.put("picUrl", picUrl);
hash.put("url", url);
data.add(hash);
第四步:创建自定义适配器
创建适配器步骤:
第一步:继承baseadapter
第二步:填写一些方法:
1 @Override 2 public int getCount() { 3 4 return data.size();//得到数据的长度 5 } 6 7 @Override 8 public Object getItem(int arg0) { 9 10 return arg0;//返回一个位置 11 } 12 13 @Override 14 public long getItemId(int arg0) { 15 // TODO Auto-generated method stub 16 return arg0;//返回一个id 17 }
1 @Override 2 public View getView(int arg0, View arg1, ViewGroup arg2) {//此处有些许的复杂 3 myHolder myholder;//缓冲机制 4 if (arg1==null) {判断view是否为空 5 myholder=new myHolder();
//得到小布局中的控件 6 arg1=LayoutInflater.from(news).inflate(R.layout.list_item,null); 7 myholder.img=(ImageView) arg1.findViewById(R.id.img); 8 myholder.content=(TextView) arg1.findViewById(R.id.content); 9 myholder.title=(TextView) arg1.findViewById(R.id.title); 10 myholder.time=(TextView) arg1.findViewById(R.id.time);
//利用setTag把查找的view缓存起来方便多次重用 11 arg1.setTag(myholder); 12 }else{
//取出setTag中缓存的view 13 myholder=(myHolder) arg1.getTag(); 14 15 }
//将数据存入hashmap中 16 HashMap<String, String> hashmap=data.get(arg0);
//17 18 19 20 21是json解析的内容 紧接上一篇博客 17 myholder.title.setText(hashmap.get("title")); 18 myholder.time.setText(hashmap.get("ctime")); 19 myholder.content.setText(hashmap.get("description")); 20 21 String img1=hashmap.get("picUrl"); 22 Picasso.with(news).load(img1).into(myholder.img); 23 24 25 return arg1; 26 } 27 28 class myHolder{ 29 TextView title; 30 TextView content; 31 ImageView img; 32 TextView time; 33 }
//adapter部分
1 package com.example.adapter; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 import com.example.z01.NewsActivity; 7 import com.example.z01.R; 8 import com.squareup.picasso.Picasso; 9 10 import android.content.Context; 11 import android.view.LayoutInflater; 12 import android.view.View; 13 import android.view.ViewGroup; 14 import android.widget.BaseAdapter; 15 import android.widget.ImageView; 16 import android.widget.TextView; 17 18 public class ListAdapter extends BaseAdapter { 19 20 ArrayList<HashMap<String, String>> data; 21 Context news; 22 23 public ListAdapter(ArrayList<HashMap<String, String>> data,Context news) { 24 this.data=data; 25 this.news=news; 26 } 27 @Override 28 public int getCount() { 29 30 return data.size(); 31 } 32 33 @Override 34 public Object getItem(int arg0) { 35 36 return arg0; 37 } 38 39 @Override 40 public long getItemId(int arg0) { 41 // TODO Auto-generated method stub 42 return arg0; 43 } 44 45 @Override 46 public View getView(int arg0, View arg1, ViewGroup arg2) { 47 myHolder myholder; 48 if (arg1==null) { 49 myholder=new myHolder(); 50 arg1=LayoutInflater.from(news).inflate(R.layout.list_item,null); 51 myholder.img=(ImageView) arg1.findViewById(R.id.img); 52 myholder.content=(TextView) arg1.findViewById(R.id.content); 53 myholder.title=(TextView) arg1.findViewById(R.id.title); 54 myholder.time=(TextView) arg1.findViewById(R.id.time); 55 arg1.setTag(myholder); 56 }else{ 57 myholder=(myHolder) arg1.getTag(); 58 59 } 60 HashMap<String, String> hashmap=data.get(arg0); 61 myholder.title.setText(hashmap.get("title")); 62 myholder.time.setText(hashmap.get("ctime")); 63 myholder.content.setText(hashmap.get("description")); 64 65 String img1=hashmap.get("picUrl"); 66 Picasso.with(news).load(img1).into(myholder.img); 67 68 69 return arg1; 70 } 71 72 class myHolder{ 73 TextView title; 74 TextView content; 75 ImageView img; 76 TextView time; 77 } 78 }
json解析以及listview 接送解析紧跟上一篇博客
1 public class NewsActivity extends Activity { 2 @ViewInject(value = R.id.news_list) 3 private ListView list; 4 private ArrayList<HashMap<String, String>> data; 5 HashMap<String, String> hash; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 // TODO Auto-generated method stub 10 super.onCreate(savedInstanceState); 11 12 setContentView(R.layout.news); 13 ViewUtils.inject(NewsActivity.this); 14 data = new ArrayList<HashMap<String, String>>(); 15 myList(); 16 Log.e("aa","a1"); 17 18 list.setOnItemClickListener(new OnItemClickListener() { 19 20 @Override 21 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 22 long arg3) { 23 24 HashMap<String, String> map1 = data.get(arg2); 25 String name = map1.get("url"); 26 Intent intent = new Intent(NewsActivity.this, UrlActivity.class); 27 intent.putExtra("zwn", name); 28 startActivity(intent); 29 Log.e("aa","a2"); 30 31 } 32 }); 33 ListAdapter adapter = new ListAdapter(data, NewsActivity.this); 34 list.setAdapter(adapter); 35 Log.e("aa","a3"); 36 37 } 38 39 private void myList() { 40 41 // 打开文件将文件转化为字符流 42 InputStream input = getResources().openRawResource(R.raw.u12); 43 Log.e("aa","a4"); 44 byte[] inputdata; 45 46 try { 47 // input.available()得到流的可利用长度 以字节的方式存储 48 inputdata = new byte[input.available()]; 49 50 // 将流读入字节数组里 51 // output.write(inputdata) 将字节数组里面缓存的数据写到output所指向的那个文件中去。 52 input.read(inputdata); 53 // 关闭流 54 input.close(); 55 // 将byte字节数组转化为字符串 56 String bytedata = new String(inputdata,"GBK"); 57 try { 58 JSONObject objec = new JSONObject(bytedata); 59 String aa = objec.getString("code"); 60 String bb = objec.getString("msg"); 61 62 JSONArray jsonarray=objec.getJSONArray("newslist"); 63 for (int i = 0; i < inputdata.length; i++) { 64 65 JSONObject objec1 = jsonarray.getJSONObject(i); 66 String ctime = objec1.getString("ctime"); 67 String title = objec1.getString("title"); 68 String description = objec1.getString("description"); 69 String picUrl = objec1.getString("picUrl"); 70 String url = objec1.getString("url"); 71 hash = new HashMap<String, String>(); 72 hash.put("ctime", ctime); 73 hash.put("title", title); 74 hash.put("description", description); 75 hash.put("picUrl", picUrl); 76 hash.put("url", url); 77 data.add(hash); 78 79 } 80 81 } catch (JSONException e1) { 82 // TODO Auto-generated catch block 83 e1.printStackTrace(); 84 } 85 86 87 } catch (IOException e) { 88 // TODO Auto-generated catch block 89 e.printStackTrace(); 90 } 91 92 } 93 }