• ListView之二。


    activity_main.xml:

    <RelativeLayout  

            android:width="match_parent"

            android:height="60dp"

            android:background="@android:color/holo_blue_bright">

    <Button 

           android:id= "@+id/btnBack"

           android:centerVertical="true"

           android:text="返回"

           android:width="wrap_content"

           android:height="wrap_content"

    android:visibility = "gont"/>

    <TextView

           android:width="wrap_content"

           android:height="wrap_content"

           android:text="学生列表"

           android:textSize="20sp"      

           android:centerVertical="true" 

          android:centerHorizontal="true"/>

    </RelativeLayout>

    <ListView 

          android:layout_width="wrap_content"

          android:layout_height = "wrap_content"

          android:id = "@+id/listViewStudent"

    ></ListView>

    MainActivity.java:

         private ListView listViewStudent;

         this.listViewStudent = (ListView)this.findViewById(R.id.listViewStudent);

         StudentAdapter studentAdapter = new StudentAdapter(this.getData(),this);

         this.listViewStudent.setAdapter(studentAdapter);

         

         private List<Student> getData(){

          List<Student> data = new ArrayList<>();

          for(int idx = 0;idx<30;idx++){

             Student student = new Student();

             student.setHeight(160 + idx);

             student.setName("杨森");

             if(idx % 2 ==0){

            student.setSex("女");

    }

    else{

            student.setSex("男");

    }       

             data.add(student);

         }

    return data;

    }

    StudentAdapter.java extends BaseAdapter:

        public studentAdapter(List<Student>,Activity activity){

              this.studentList = studentList;

              this.activity = activity;

    }

        private List<Student> studentList;

        private Activity activity;

    /*
    显示Item的数量
    */
    @Override
    public int getCount() {
    System.out.println("-----getCount-----");
    return this.studentList.size();
    }
    @Override
    public Object getItem(int position) {
        System.out.println("-----getItem-----");
        return this.studentList.get(position);
    }
    @Override
    public long getItemId(int position) {
        System.out.println("-----getItemId-----",+position);
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    //System.out.println("-----getView----"+position);
    Student student = this.studentList.get(position);

    TextView tvName=(TextView)findViewById(R.id.tvName);
    TextView tvHeight=(TextView)findViewById(R.id.tvHeight);
    TextView tvSex=(TextView)findViewById(R.id.tvSex);
    tvName.setName(student.getName());
    tvheight.setName(student.getHeight());
    tvSex.setName(student.getSex());


    View view = View.inflate(this.activity,R.layout.listview_item_student,null);
    return view;
    }
  • 相关阅读:
    C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出
    C# 中对 IEnumerable IEnumerator yield 的理解
    C 语言 用字符输出菱形图案的函数(可自定义边长及字符样式)
    C 语言 对角线添充二维数组
    C 语言 边读 边写入文件
    [转]Page.RegisterRequiresRaiseEvent()与Page.RegisterRequiresPostBack()
    asp.net 判断是手机或电脑访问网页的方式
    表达式树【转】
    ADO.NET中的“返回多个结果集”和“MARS”【转】
    SQL2005转2000的方法【转】
  • 原文地址:https://www.cnblogs.com/xiaoxiaomeng/p/5682734.html
Copyright © 2020-2023  润新知