码迷,mamicode.com
首页 > 移动开发 > 详细

Android Studio学习路程(12)

时间:2020-03-02 01:13:22      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:size   line   adapter   code   extc   end   override   res   ring   

今天学习了一个控件:ListView

package com.example.hp.listview.Activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.hp.listview.R;
import com.example.hp.listview.adapter.MyAdapter;
import com.example.hp.listview.bean.Student;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    private ListView mListView;
    private List<Student> mStudents;
    private MyAdapter mMyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化UI
        initUI();
        //初始化数据集
        initData();
    }
    private void initData() {
        mStudents = new ArrayList<>();

        for(int i=0;i<100;i++){
            mStudents.add(new Student(i, "学生" + i, "描述信息" + i));
        }
        //实例化一个适配器
        mMyAdapter = new MyAdapter(mStudents,getApplicationContext());
        //设置构造器
        mListView.setAdapter(mMyAdapter);

    }

    private void initUI() {
        mListView = (ListView) findViewById(R.id.list_view);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity.MainActivity">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>
package com.example.hp.listview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.hp.listview.R;
import com.example.hp.listview.bean.Student;

import java.util.List;

/**
 * Created by hp on 2020/3/1.
 */
public class MyAdapter extends BaseAdapter {

    private final List<Student> mStudents;
    private final Context context;
    private TextView name;
    private TextView des;
    public MyAdapter(List<Student> students, Context context) {
        mStudents = students;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mStudents.size();
    }

    @Override
    public Object getItem(int i) {
        return mStudents.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mStudents.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view==null){
            view=View.inflate(context, R.layout.item_list,null);
        }
        //将数据集合设置到每一个条目中
        name = (TextView) view.findViewById(R.id.tv_name);
        des = (TextView) view.findViewById(R.id.tv_des);
        //设置姓名
        name.setText(mStudents.get(i).getName());
        //设置描述信息
        des.setText(mStudents.get(i).getDes());
        return view;
    }
}
package com.example.hp.listview.bean;

/**
 * Created by hp on 2020/3/1.
 */
public class Student {
    private int id;//学生的id
    private String name;//姓名
    private String des;//描述

    public Student(int id, String name, String des) {
        this.id = id;
        this.name = name;
        this.des = des;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
public void setName(String name) {
        this.name = name;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:padding="8dp"
              android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_name"
        android:textSize="24sp"
        android:text="姓名"
        android:textColor="@android:color/holo_red_light"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_des"
        android:textSize="24sp"
        android:text="描述信息"
        android:textColor="@android:color/holo_blue_bright"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

</LinearLayout>

下面是运行的结果截图:

技术图片

 

 技术图片

 

 这里呢有一个MyAdapter是一个适配器·。

Android Studio学习路程(12)

标签:size   line   adapter   code   extc   end   override   res   ring   

原文地址:https://www.cnblogs.com/mxk123456/p/12392997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!