public class MyListView4 extends ListActivity {
private List<Map<String, Object>> mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = getData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("info", "google 1");
map.put("img","url1" );
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G2");
map.put("info", "google 2");
map.put("img", "url2");
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G3");
map.put("info", "google 3");
map.put("img", "url3");
list.add(map);
return list;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.v("MyListView4-click", (String)mData.get(position).get("title"));
}
public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("我的listview")
.setMessage("介紹...")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = mInflater.inflate(R.layout.vlist2, null);
ImageView img = (ImageView)view.findViewById(R.id.img);
TextView title = (TextView)view.findViewById(R.id.title);
TextView info = (TextView)view.findViewById(R.id.info);
Button viewBtn = (Button)view.findViewById(R.id.view_btn);
img.setBackgroundResource((Integer)mData.get(position).get("img"));
title.setText((String)mData.get(position).get("title"));
info.setText((String)mData.get(position).get("info"));
viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInfo();
}
});
return view;
}
}
listview的優化,主要是在adapter的getview中1.復用view減少findViewById的次數 下面只供參考、、、
public View getView(int position, View convertView, ViewGroup parent) {
MyHolder holder;
if(convertView==null){
convertView =
LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
holder = new MyHolder();
holder.artist =
(TextView) convertView.findViewById(R.id.item_artist);
holder.duration =
(TextView) convertView.findViewById(R.id.item_duration);
convertView.setTag(holder);
}else{
holder = (MyHolder) convertView.getTag();
}
Music m = getItem(position);
holder.artist.setText(m.getArtist()); holder.duration.setText(dateFormat.format(new
Date(m.getDuration())));
return convertView;
}}
public class MyHolder{
private TextView title;
private TextView artist;
private TextView duration;
}