0x00 Preface
Before that, I learned ListView and three adapters, arrayadapter, simpleadapter and simplecusoradapter in Android
This time we mainly focus on BaseAdapter to learn and practice.
0x01 what is BaseAdapter
In short, it is a kind of universal adapter, which we usually inherit.
We will implement it after inheriting
public int getCount(): the number of data sets in the adapter;
public Object getItem(int position): get the data item corresponding to the index in the dataset;
public long getItemId(int position): gets the ID corresponding to the specified row;
public View getView(int position,View convertView,ViewGroup parent): get the display content of no line Item.
After reading these, maybe we don't understand very well, so we need to raise a chestnut at this time.
0x02 example
Code
Main main activity
package com.example.hanlei.baseadapterdemo;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private List<Animal> mdata=null;
private Context mContext;
private AnimalAdapter mAdapter =null;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LayoutInflater inflater=LayoutInflater.from(this);
View head=inflater.inflate(R.layout.view_header,null,false);
View end=inflater.inflate(R.layout.view_header,null,false);
mContext=MainActivity.this;
listView=findViewById(R.id.lv);
mdata=new LinkedList<Animal>();
mdata.add(new Animal("The dog said","Are you a dog?",R.mipmap.p1));
mdata.add(new Animal("The dog said","Are you a dog?",R.mipmap.p2));
mdata.add(new Animal("The dog said","Are you a dog?",R.mipmap.p3));
mdata.add(new Animal("The dog said","Are you a dog?",R.mipmap.p4));
mAdapter= new AnimalAdapter((LinkedList<Animal>)mdata, mContext);
listView.addHeaderView(head);
listView.addFooterView(end);
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(mContext,"You clicked the"+i+"term",Toast.LENGTH_SHORT).show();
}
}
class
package com.example.hanlei.baseadapterdemo;
/**
* Created by hanlei on 2018/4/4.
*/
public class Animal {
private String aName;
private String aSpeak;
private int aIcon;
public Animal()
{
}
public Animal(String aName,String aSpeak,int aIcon)
{
this.aName=aName;
this.aSpeak=aSpeak;
this.aIcon=aIcon;
}
public int getaIcon() {
return aIcon;
}
public String getaName() {
return aName;
}
public String getaSpeak() {
return aSpeak;
}
public void setaIcon(int aIcon) {
this.aIcon = aIcon;
}
public void setaName(String aName) {
this.aName = aName;
}
public void setaSpeak(String aSpeak) {
this.aSpeak = aSpeak;
}
}
BaseAdapter
package com.example.hanlei.baseadapterdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.LinkedList;
/**
* Created by hanlei on 2018/4/4.
*/
public class AnimalAdapter extends BaseAdapter {
private LinkedList <Animal> mData;
private Context mContext;
public AnimalAdapter(LinkedList<Animal> mData,Context mContext){
this.mData=mData;
this.mContext=mContext;
}
//Number of data
@Override
public int getCount() {
return mData.size();
}
//Gets the data item of the data set corresponding to the specified index
@Override
public Object getItem(int i) {
return null;
}
//Corresponding row ID
@Override
public long getItemId(int i) {
return i;
}
//Get the content of each Item
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view= LayoutInflater.from(mContext).inflate(R.layout.demo,viewGroup,false);
ImageView imageView=view.findViewById(R.id.imgtou);
TextView textView=view.findViewById(R.id.name);
TextView textView1=view.findViewById(R.id.says);
imageView.setBackgroundResource(mData.get(i).getaIcon());
textView.setText(mData.get(i).getaName());
textView1.setText(mData.get(i).getaSpeak());
return view;
}
}