自定义LisetView

1、ListView listview=findViewById(R.id.listview);

2、public class MyAdapter extends BaseAdapter{

  private LayoutInflater myLayoutInflater =LayoutInflater.from(context);//用来绑定需要设定的Item.xml

  public MyAdapter(){}//构造函数,传入值

  public class Info {//存放每个listview中的控件

    public TextView textview;
  }

  //再重写四个函数

  @Override
  public int getCount() {
  return car_num_str.length;//返回需要展示多少个item
  }

  @Override
  public Object getItem(int position) {
  return position;
  }

  @Override
  public long getItemId(int position) {
  return position;
  }

  @Override//重点
  public View getView(int position, View convertView, ViewGroup parent) {
  Info info = null;
  if (convertView == null) {
  Info = new Info();
  convertView = myLayoutInflater.inflate(R.layout.item, null);
  info.textview = (TextView) convertView.findViewById(R.id.textview);
  convertView.setTag(info);
  } else {
  info = (Info) convertView.getTag();
    }
  info.textview.setText(Stringtv[position]);//将外部传进来的车牌号放在listview中
  return convertView;
  }

  }

3、MyAdapter myAdapter = new MyAdapter();//得到适配器对象

4、listview.setAdapter(myAdapter);//添加适配器

posted @ 2016-08-20 15:44  朱小勇  阅读(336)  评论(0编辑  收藏  举报