大禹派吧 关注:38贴子:5,409

回复:android学习记录

只看楼主收藏回复

m MODE 数据模型 List<Person>
v view 视图 ListView
c controller 控制器 adapter适配器


IP属地:北京65楼2014-08-12 17:04
回复
    用listview实现63楼效果
    在布局文件中加入
    <ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    </ListView>
    onCreat方法中lv.setAdapter(new MyAdapter());
    创建一个内部类
    private class MyAdapter extends BaseAdapter{
    private static final String TAG = "MyAdapter";
    /**
    * 控制listview里有多少条数据
    */
    @Override
    public int getCount() {
    return persons.size();
    }
    @Override
    public Object getItem(int position) {
    return null;
    }
    @Override
    public long getItemId(int position) {
    return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    TextView tv = new TextView(getApplicationContext());
    Person person = persons.get(position);
    tv.setText(person.toString());
    return tv;
    }
    }


    IP属地:北京66楼2014-08-12 18:14
    回复
      public View getView(int position, View convertView, ViewGroup parent) {
      Person person = persons.get(position);
      View view = View.inflate(MainActivity.this, R.layout.list_item, null);
      //一定要在view对象中找孩子的id
      TextView tv_id = (TextView)view.findViewById(R.id.tv_id);
      tv_id.setText("id:" + person.getId());
      填充布局文件


      IP属地:北京69楼2014-08-12 20:51
      回复