Android Studio学习路程(12)

Posted mashuxia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio学习路程(12)相关的知识,希望对你有一定的参考价值。

今天学习了一个控件: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)的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio学习路程

Android Studio学习路程(11)

Android Studio学习路程

Android Studio学习路程(10)

Android Studio学习路程

Android Studio学习路程(13)