package com.example.basadper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
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 Activity {
private ArrayList<Map<String,Object>>data;
private ListView lV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lV = (ListView) findViewById(R.id.listView1);
MyAdaper adaper = new MyAdaper(this);
lV.setAdapter(adaper);
}
public ArrayList<Map<String,Object>> getdata(){
ArrayList<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for(int i=0;i<=100;i++){
Map<String,Object> map1 = new HashMap<String,Object>();
map1.put("img", R.id.imageView1);
map1.put("TextView", R.id.textView1);
map1.put("TextView", R.id.textView2);
list.add(map1);
}
return list;
}
static class ViewHolder
{
public ImageView img;
public TextView title;
public TextView info;
}
class MyAdaper extends BaseAdapter{
private LayoutInflater mInflater = null;
private MyAdaper(Context context)
{
//根据context上下文加载布局,这里的是Demo17Activity本身,即this
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return getdata().size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return 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 holder = null;
//如果缓存convertView为空,则需要创建View
if(convertView == null)
{
holder = new ViewHolder();
//根据自定义的Item布局加载布局
convertView =
holder.img = (ImageView)convertView.findViewById(R.id.imageView1);
holder.title = (TextView)convertView.findViewById(R.id.textView1);
holder.info = (TextView)convertView.findViewById(R.id.textView2);
//将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag
convertView.setTag(holder);
}else
{
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)data.get(position).get("img"));
holder.title.setText((String)data.get(position).get("title"));
holder.info.setText((String)data.get(position).get("info"));
return convertView;
}
}
}
Robey
qq_匡璐_0
liu丶
拟人
相关分类