public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)
this
.findViewById(R.id.listView1);
ArrayList<HashMap<String,String>> listSource =
new
ArrayList<HashMap<String,String>>();
for
(
int
num=
1
; num<
10
; num++)
{
HashMap<String,String> map =
new
HashMap<String, String>();
map.put(
"photo"
, String.valueOf(R.drawable.ic_launcher));
map.put(
"num"
,String.valueOf(num));
map.put(
"name"
,
"stu"
+num );
map.put(
"className"
,
"software 2"
);
listSource.add(map);
}
lv.setAdapter(
new
MyListViewAdapter(listSource));
}
public
class
MyListViewAdapter
extends
BaseAdapter{
View[] itemViews;
public
MyListViewAdapter(ArrayList<HashMap<String,String>> listSource)
{
itemViews =
new
View[listSource.size()];
Iterator<HashMap<String, String>> it = listSource.iterator();
HashMap<String,String> hash;
int
i =
0
;
try
{
while
(it.hasNext())
{
hash = it.next();
itemViews[i] = makeItemView(hash);
i++;
}
}
catch
(Exception e){
Log.e(
"Error"
, e.getMessage());
e.printStackTrace();
}
}
private
View makeItemView(HashMap<String,String> map) {
if
(map ==
null
|| map.isEmpty())
return
null
;
LayoutInflater inflater = (LayoutInflater) MainActivity.
this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listviewitem,
null
);
TextView title = (TextView) itemView.findViewById(R.id.textView1);
title.setText(map.get(
"num"
));
Log.e(
"num"
, map.get(
"num"
));
TextView text = (TextView) itemView.findViewById(R.id.textView2);
text.setText(map.get(
"name"
));
Log.e(
"name"
, map.get(
"name"
));
TextView text2 = (TextView) itemView.findViewById(R.id.textView3);
text2.setText(map.get(
"className"
));
Log.e(
"className"
, map.get(
"className"
));
ImageView image = (ImageView) itemView.findViewById(R.id.imageView1);
image.setImageResource(Integer.parseInt(map.get(
"photo"
)));
Log.e(
"photo"
, map.get(
"photo"
));
return
itemView;
}
@Override
public
int
getCount() {
return
itemViews.length;
}
@Override
public
Object getItem(
int
position) {
return
itemViews[position];
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
if
(convertView ==
null
)
return
itemViews[position];
return
convertView;
}}
}
}