ArrayAdapter:
MyAdapter自定义适配器:
实体类
public class ArrayAdapterActivity extends Activity {
ListView listview=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_adapter);
listview =(ListView) findViewById(R.id.listView);
List<String> mylist =new ArrayList<String>();
mylist.add("湖南");
mylist.add("湖北");
mylist.add("天剑");
mylist.add("上海");
mylist.add("广州");
mylist.add("深圳");
mylist.add("黑龙江");
mylist.add("江西");
mylist.add("江苏");
mylist.add("济南");
mylist.add("台湾");
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1, mylist);
listview.setAdapter(adapter);
// listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ListView l=(ListView) parent;
String name=(String) l.getItemAtPosition(position);
Toast.makeText(ArrayAdapterActivity.this, name, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.array_adapter, menu);
return true;
}
}
SimpleAdapter:
public class SimpleAdapterActivity extends Activity {
ListView listview =null;
List<Map<String, Object>> list=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_adapter);
listview=(ListView) findViewById(R.id.listView);
list=getData();
String [] from=new String[]{"image","gameName","gameDownloadNumber"};
int [] to= new int []{R.id.image,R.id.gameName,R.id.gameDownloadNumber};
SimpleAdapter simpleadapter= new SimpleAdapter(this, list, R.layout.simpleadaterlist, from, to);
listview.setAdapter(simpleadapter);
}
public class Myonclicklistener implements OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
ListView listview =(ListView) parent;
Map<String, Object>map=(Map<String, Object>) parent.getItemAtPosition(position);
String name=(String) map.get("gameName");
String gameDownloadNumber=(String) map.get("gameDownloadNumber");
Toast.makeText(getApplicationContext(), "gameName="+name+"gameDownloadNumber"+gameDownloadNumber, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> games =new ArrayList<Map<String,Object>>();
Random s= new Random();
Map<String, Object> game= new HashMap<String, Object>();
game.put("image", R.drawable.help2);
game.put("gameName", "传奇世界");
game.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game1= new HashMap<String, Object>();
game1.put("image", R.drawable.html);
game1.put("gameName", "传奇私服");
game1.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game2= new HashMap<String, Object>();
game2.put("image", R.drawable.html3);
game2.put("gameName", "泡泡");
game2.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game3= new HashMap<String, Object>();
game3.put("image", R.drawable.iphone1);
game3.put("gameName", "跑跑卡丁车");
game3.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game4= new HashMap<String, Object>();
game4.put("image", R.drawable.java1);
game4.put("gameName", "斗破");
game4.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game5= new HashMap<String, Object>();
game5.put("image", R.drawable.java2);
game5.put("gameName", "梦幻西游");
game5.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game6= new HashMap<String, Object>();
game6.put("image", R.drawable.java3);
game6.put("gameName", "地下城与勇士");
game6.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game7= new HashMap<String, Object>();
game7.put("image", R.drawable.java4);
game7.put("gameName", "英雄联盟");
game7.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game8= new HashMap<String, Object>();
game8.put("image", R.drawable.java5);
game8.put("gameName", "极光世界");
game8.put("gameDownloadNumber", s.nextInt(1000));
Map<String, Object> game9= new HashMap<String, Object>();
game9.put("image", R.drawable.linux1);
game9.put("gameName", "古古传奇");
game9.put("gameDownloadNumber", s.nextInt(1000));
games.add(game);
games.add(game1);
games.add(game2);
games.add(game3);
games.add(game4);
games.add(game5);
games.add(game6);
games.add(game7);
games.add(game8);
games.add(game9);
return games;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.simple_adapter, menu);
return true;
}
}
MyAdapter自定义适配器:
public class MyAdapterActivity extends Activity {
ListView listview =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_adapter);
listview=(ListView) findViewById(R.id.listView);
listview.setAdapter(new MyAdapter(this));
}
public class MyAdapter extends BaseAdapter
{
Context context;
List games=null;
LayoutInflater layoutinflater=null;
public MyAdapter(Context context)
{
this.context=context;
games=getData();
layoutinflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return games.size();
}
@Override
public Object getItem(int position) {
return games.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
final Game game=(Game) games.get(position);
ImageView image=null;
TextView title=null;
TextView number=null;
Button click=null;
if(view==null)
{
View view1=layoutinflater.inflate(R.layout.simpleadaterlist, null);
image=(ImageView) view1.findViewById(R.id.image);
title=(TextView) view1.findViewById(R.id.gameName);
number=(TextView) view1.findViewById(R.id.gameDownloadNumber);
click=(Button) view1.findViewById(R.id.btn_download);
System.out.println("建立view");
view=view1;
Component com= new Component();
com.image=image;
com.title=title;
com.number=number;
com.click=click;
view.setTag(com);
System.out.println("view ="+view);
}
Component comp= (Component) view.getTag();
System.out.println("view ="+view);
comp.image.setImageResource(game.getImage());
comp.title.setText(game.getName());
comp.number.setText(game.getDownloadNumber());
comp.click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(context, "name="+game.getName(), Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
public class Component{
ImageView image;
TextView title;
TextView number;
Button click;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_adapter, menu);
return true;
}
public List getData() {
List l= new ArrayList<Game>();
Random r= new Random();
for (int i = 0; i < 20; i++) {
Game game=new Game("游戏",R.drawable.help2+i%12,r.nextInt(10000));
l.add(game);
}
return l;
}
}
实体类
public class Game {
String name;
int downloadNumber;
int image;
public Game(String name, int downloadNumber, int image) {
super();
this.name = name;
this.downloadNumber = downloadNumber;
this.image = image;
}
public Game() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDownloadNumber() {
return downloadNumber;
}
public void setDownloadNumber(int downloadNumber) {
this.downloadNumber = downloadNumber;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
@Override
public String toString() {
return "Game [name=" + name + ", downloadNumber=" + downloadNumber
+ ", image=" + image + "]";
}
}
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyAdapterActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
<?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" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/gameName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/gameDownloadNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
/>
</LinearLayout>
<Button
android:id="@+id/btn_download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:text="@string/download"
/>
</LinearLayout>