获取数据
创建适配器
绑定适配器
需要实现重写的函数:
getCount() 获取数据长度
getItem (int position) 获取项资源
getItemId(int position) 返回解析数据项ID
getView () 把项资源解析成可见视图
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
//需要适配的数据
private String[] names = {"京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫",
"UC浏览器", "微信"};
//图片集合
private int[] icons = {R.drawable.jd, R.drawable.qq, R.drawable.dz,
R.drawable.xl, R.drawable.tm, R.drawable.uc,R.drawable.wx};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化ListView控件
mListView = (ListView) findViewById(R.id.lv);
//创建一个Adapter的实例
MyBaseAdapter mAdapter = new MyBaseAdapter();
//设置Adapter
mListView.setAdapter(mAdapter);
}
class MyBaseAdapter extends BaseAdapter {
//得到item的总数
@Override
public int getCount() {
//返回ListView Item条目的总数
return names.length;
}
//得到Item代表的对象
@Override
public Object getItem(int position) {
//返回ListView Item条目代表的对象
return names[position];
}
//得到Item的id
@Override
public long getItemId(int position) {
//返回ListView Item的id
return position;
}
//得到Item的View视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getApplicationContext()).
inflate(R.layout.list_item,parent,false);
holder = new ViewHolder();
holder.mTextView = (TextView)convertView.findViewById
(R.id. item_tv);
holder.imageView=(ImageView) convertView.findViewById(R.id.item_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(names[position]);
holder.imageView.setBackgroundResource(icons[position]);
return convertView;
}
class ViewHolder {
TextView mTextView;
ImageView imageView;
}
}
}
父适配器:
继承自BaseAdapter
package com.example.androidapplicationmarket;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class DefaultAdapter<T> extends BaseAdapter {
private String Info[]=null;
Context context;
public DefaultAdapter(String[] Info, Context context) {
this.Info = Info;
this.context = context;
}
public DefaultAdapter() {
super();
}
@Override
public int getCount() {
return Info.length;
}
@Override
public Object getItem(int position) {
return Info[position];
}
@Override
public long getItemId(int position) {
return position;
}
public abstract BaseHolder<T>getHolder();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BaseHolder<T>holder=null;
if (convertView == null) {
holder=getHolder();
//holder.initView();
convertView=holder.getView();
}
holder=(BaseHolder<T>)convertView.getTag();
holder.initData(position);
return convertView;
}
}
自定义适配器:
为方便使用,隐藏四个需要重写的函数,只需继承父适配器
package com.example.androidapplicationmarket;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends DefaultAdapter {
public MyAdapter(String[] Info, Context context,String names[],int icons[]) {
super(Info, context);
this.names=names;
this.icons=icons;
}
//需要适配的数据
private String[] names ;
//图片集合
private int[] icons ;
//
// public String[] getNames() {
// return names;
// }
//
// public void setNames(String[] names) {
// this.names = names;
// }
//
// public int[] getIcons() {
// return icons;
// }
//
// public void setIcons(int[] icons) {
// this.icons = icons;
// }
@Override
public BaseHolder getHolder() {
return new MyHolder();
}
private class MyHolder extends BaseHolder{
//绑定数据
private TextView tv;
private ImageView iv;
@Override
public void initView() {
view= View.inflate(context,R.layout.list_item,null);
tv=(TextView)view.findViewById(R.id.item_tv);
iv=(ImageView)view.findViewById(R.id.item_image);
view.setTag(this);
}
@Override
public void initData(int position) {
tv.setText(names[position]);
iv.setBackgroundResource(icons[position]);
}
}
}
package com.example.androidapplicationmarket;
import android.view.View;
public abstract class BaseHolder<T> {
protected View view=null;
public BaseHolder(){
initView();
}
public abstract void initView() ;
public View getView() {
return view;
}
public abstract void initData(int position) ;
}
绑定数据模板类(数据初始化)
package com.example.androidapplicationmarket;
import android.view.View;
public abstract class BaseHolder<T> {
protected View view=null;
public BaseHolder(){
initView();
}
public abstract void initView() ;
public View getView() {
return view;
}
public abstract void initData(int position) ;
}
界面交互函数
使用自定义适配器
package com.example.androidapplicationmarket;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
//需要适配的数据
private String[] names = {"京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫",
"UC浏览器", "微信"};
//图片集合
private int[] icons = {R.drawable.jd, R.drawable.qq, R.drawable.dz,
R.drawable.xl, R.drawable.tm, R.drawable.uc,R.drawable.wx};
//MyAdapter myAdapter=new MyAdapter(names,icons);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化ListView控件
mListView = (ListView) findViewById(R.id.lv);
//创建一个Adapter的实例
MyAdapter myAdapter = new MyAdapter(names,this,names,icons);
//设置Adapter
mListView.setAdapter(myAdapter);
}
}