关于Android的ListView一点使用方法

  在物联网学习中我需要用到Android来获取PC端数据库内的表数据并显示在屏幕上,因此我决定用WebService来连接数据库获取数据,用ListView组件显示数据。这里我将获取到的数据存入List泛型中,就不演示如何获取数据。

  ListView是Android常用的一个组件,可以以列表的方式显示数据,并且能自适应内容。ListView组件并非一次加载所有数据,而是屏幕显示多少行数据就加载多少行数据。但往下拖动时再加载新的一行数据,且当屏幕第一行被移出屏幕时内存会被回收,往下同理。所以当数据量很大时不用担心用ListView会出问题。

  ListView使用setAdapter()方法来绑定数据适配器,这里我选择 使用一个类来继承BaseAdapter,BaseAdapter类有四个必须实现的方法,getCount(),getView(),getItem(),getItemId()。其中,重要的方法为getCount()和getView()

public class Myadapter extends BaseAdapter{
    @Override
    public int getCount() {
        return null; //返回数据的总行数
    }
        
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return null; //返回一行数据
    }
        
    @Override
    public Object getItem(int position) {
        return null;
    }

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

  每当加载新一条数据时便会调用一次getVIew(int position, View convertView, ViewGroup parent)方法,其中position为将要加载的数据的行号。

  为了让数据显示的更好看我们可以先在res/layout文件夹下新建样式表。

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
            
         <TextView   
             android:id="@+id/ID"
             android:gravity="center"
             android:text="ID"
             android:background="#22000000"
             android:layout_width="0dp"
             android:layout_weight="1"
             android:layout_height="wrap_content"/> <!--每列的样式-->
          <View    
             android:layout_width="1dp"
             android:background="#000000"
             android:layout_height="wrap_content"/> <!--每列后的竖线-->

         <TextView 
             android:id="@+id/NAME"
             android:gravity="center"
             android:background="#22000000"
             android:layout_width="0dp"
             android:layout_weight="1"
             android:layout_height="wrap_content"
             android:text="NAME"/>
          <View 
             android:layout_width="1dp"
             android:background="#000000"
             android:layout_height="wrap_content"/>
          <TextView 
             android:id="@+id/PASSWORD"
             android:gravity="center"
             android:background="#22000000"
             android:layout_width="0dp"
             android:layout_weight="1"
             android:layout_height="wrap_content"
             android:text="PASWORD"/>
</LinearLayout>

  定义好每一行的View样式后我们回到getView()中方法中

public View getView(int position, View convertView, ViewGroup parent) {
    View view =View.inflate(nextActivity.this, R.layout.list_resoure, null);//R.layout.list_resoure为刚刚定义好的样式
    TextView tv_id = (TextView) view.findViewById(R.id.ID);
    TextView tv_name = (TextView) view.findViewById(R.id.NAME);
    TextView tv_psd = (TextView) view.findViewById(R.id.PASSWORD);//获取到list_resoure中的控件
    tv_id.setText(userInfo.get(position).getId());
    tv_name.setText(userInfo.get(position).getName());
    tv_psd.setText(userInfo.get(position).getPassword());//为list_resoure中的控件赋值,userInfo为所有要显示的数据的List泛型
    return view;//返回这一行数据显示在屏幕上
}

  不要忘记在getCount()方法中返回数据的总行数

@Override
public int getCount() {
    return userInfo.size();//返回值为int类型,表示数据的总行数
}

  getItem()和getItemId()方法可无视。

  最后,使用ListView的setAdapter来绑定我们定义好的数据适配器Myadpter了

ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new Myadapter());

  

原文地址:https://www.cnblogs.com/LandMine/p/4457821.html