ListView的适配(使用自定义ArrayAdapter)
来源:互联网 发布:双11淘宝客服工作总结 编辑:程序博客网 时间:2024/06/05 04:02
ListView在平时开发中使用的最广泛,应用也是最多的,而使用系统的适配器并不能满足我们多样化的要求,因此本文就自定义适配器展开,下面直接附上我已经测试通过的代码:
MainActivity.java:
package com.example.listviewpushtorefresh;import java.util.ArrayList;import java.util.List;import bean.Person;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;public class MainActivity extends Activity { private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview); MyAdapter adapter = new MyAdapter(MainActivity.this,0,getData()); listview.setAdapter(adapter); } private List<Person> getData() { // TODO Auto-generated method stub List<Person> list= new ArrayList<Person>(); for(int i=0;i<20;i++) { Person p = new Person(R.drawable.ic_launcher,i+"","我是第"+i+"项"); list.add(p); } return list; }}
自定义数据类型:Person.java
package bean;public class Person { private int mPicture; private String mTitle; private String mContent; public Person(int picture,String title,String content){ this.mPicture = picture; this.mTitle = title; this.mContent = content; }public int getPicture() { return mPicture;}public void setmPicture(int mPicture) { this.mPicture = mPicture;}public String getTitle() { return mTitle;}public void setTitle(String mTitle) { this.mTitle = mTitle;}public String getContent() { return mContent;}public void setContent(String mContent) { this.mContent = mContent;}}
自定义适配器:MyAdapter.java
package com.example.listviewpushtorefresh;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.TextView;import bean.Person;public class MyAdapter extends ArrayAdapter<Person> { private int resoureId; private List<Person> objects; private Context context; public MyAdapter(Context context, int resourceId, List<Person> objects) { super(context, resourceId, objects); // TODO Auto-generated constructor stub this.objects=objects; this.context=context; } private static class ViewHolder { ImageView imageView; TextView title; TextView content; } @Override public int getCount() { // TODO Auto-generated method stub return objects.size(); } @Override public Person getItem(int position) { // TODO Auto-generated method stub return objects.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder viewHolder = null; if(convertView==null) { viewHolder=new ViewHolder(); LayoutInflater mInflater=LayoutInflater.from(context); convertView = mInflater.inflate(R.layout.item, null); viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageview); viewHolder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.content = (TextView) convertView.findViewById(R.id.content); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Person person = objects.get(position); if(null!=person) { viewHolder.imageView.setBackgroundResource(person.getPicture()); viewHolder.title.setText(person.getTitle()); viewHolder.content.setText(person.getContent()); } return convertView; }}
主布局文件:main.xml
<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" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview" android:layout_margin="2dp" > </ListView></RelativeLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="#a4d3ee"> <ImageView android:id="@+id/imageview" android:layout_width="50dp" android:layout_weight="0" android:layout_height="wrap_content" android:background="@drawable/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp"> <TextView android:id="@+id/title" android:layout_width="150dp" android:layout_height="wrap_content" android:text="title" android:textSize="20sp"/> <TextView android:id="@+id/content" android:layout_width="150dp" android:layout_height="wrap_content" android:text="content"/> </LinearLayout></LinearLayout>
1 0
- ListView的适配(使用自定义ArrayAdapter)
- ListView的适配(使用自定义ArrayAdapter)
- ListView的使用-ArrayAdapter
- ListView之ArrayAdapter的使用
- ListView之ArrayAdapter的使用
- radiobtn的listview,自定义arrayAdapter构成样式
- 自定义ArrayAdapter+ListView实现笔记本的App
- 自定义listview的ArrayAdapter报空指针
- 使用定制的ArrayAdapter制作ListView的Items(翻译)
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- Android 基本控件的使用五(ListView 与 ArrayAdapter)
- 安卓开发(4):ListView的使用,arrayAdapter,simpleAdapter
- ListView的Adapter使用 之 初学ArrayAdapter
- Android中ListView之ArrayAdapter的使用
- ListView的使用----ArrayAdapter与SimpleAdapter
- Android ListView ArrayAdapter 的简单使用
- Android ListView使用ArrayAdapter
- 填充ListView(使用ArrayAdapter)
- Android APP之间共享SharedPreference
- cocos3.9 【 Sublime Text 2 配置 】
- mysql 视图创建及使用
- HTTP基本原理(简介)
- 算法 - 数组
- ListView的适配(使用自定义ArrayAdapter)
- 空间商
- Java中gitter、setter方法和public的区别
- 淘宝技术博客
- leetcode:Merge Sorted Array 【Java】
- Toolbar Toggle
- env of ubuntu dev
- 如何实现ActiveMQ的Topic的持久订阅。
- Android Studio 快捷键使用技巧(一)