package com.bivin;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainAcitivity extends Activity
{
private ListView lv;
ArrayList
Object>> datas = new
ArrayList
Object>>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HashMap
data;
for (int i = 0; i <30; i++)
{
data = new HashMap
Object>();
data.put("name", "name " + i);
data.put("age", "age " + i);
data.put("sex", "sex "+i);
datas.add(data);
}
lv = (ListView) findViewById(R.id.mylv);
Myadapter adapter = new Myadapter(this);
lv.setAdapter(adapter);
}
private class Myadapter extends BaseAdapter
{
private Context context;
public Myadapter(Context context)
{
this.context = context;
}
@Override
public int getCount()
{
return datas.size();
}
@Override
public Object getItem(int position)
{
return datas.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent)
{
ItemHolder holder = null;
if (convertView == null)
{
holder = new ItemHolder();
LayoutInflater inflater =getLayoutInflater();
convertView =inflater.inflate(R.layout.item,null);
holder.tv = (TextView)
convertView.findViewById(R.id.name);
holder.tv2 = (TextView)
convertView.findViewById(R.id.age);
holder.tv3 = (TextView)
convertView.findViewById(R.id.sex);
switch (position)
{
case 0:
holder.tv.setVisibility(View.GONE);
break;
case 1:
holder.tv.setVisibility(View.VISIBLE);
holder.tv2.setVisibility(View.GONE);
break;
case 2:
holder.tv3.setVisibility(View.GONE);
holder.tv2.setVisibility(View.VISIBLE);
break;
default:
holder.tv.setVisibility(View.VISIBLE);
holder.tv2.setVisibility(View.VISIBLE);
holder.tv3.setVisibility(View.VISIBLE);
break;
}
convertView.setTag(holder);
}else {
holder =(ItemHolder)convertView.getTag();
holder.tv.setText(datas.get(position).get("name").toString());
holder.tv2.setText(datas.get(position).get("sex").toString());
holder.tv3.setText(datas.get(position).get("age").toString());
}
return convertView;
}
}
}