Android之ListView

作者: 拨云见日aaa | 来源:发表于2019-09-28 20:26 被阅读0次

一、ListView之Adapter

  • ArrayAdapter
  • SimpleAdapter
  • 自定义Adapter

二、ArrayAdapter

(1)用法(只讲动态方法):

new ArrayAdapter<String>(上下文,只能包含一个TextView的布局文件id,包含要展示数据的数组或者list)
(当传四个参数时,第三个为TextView的id,第四个为包含要展示数据的数组或者list)

(2)代码示例

activity代码

public class MainActivity extends AppCompatActivity {
List<String> list;
ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
        lv=findViewById(R.id.lv);
       //自己创建只有Text View的布局
        ArrayAdapter<String> adapter=new ArrayAdapter<>(this,R.layout.layout,R.id.tv,list);
        //使用系统给的布局
        //ArrayAdapter<String> adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
        lv.setAdapter(adapter);
    }
    public void initList(){
        list=new ArrayList<>();
        for(int i=1;i<=20;i++) {
            list.add("第"+i+"条");
        }
    }
}

自己创的只有一个TextView的布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="70dp" />
</LinearLayout>
(3)运行结果
ArrayAdapter

三、SimpleAdapter

(1)用法

new SimpleAdapter(上下文,
先将数据封装到map里再加到list(ArrayList<HashMap<String,object>>),
自己创建的布局文件,
第二个参数里map里的键的数组,
数据所对应的View的id的数组(要和上一个参数键的顺序一样)

(2)示例代码

activity的代码

public class MainActivity extends AppCompatActivity {
ListView lv;
List<HashMap<String,Object>> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
        lv=findViewById(R.id.lv);
        String[] key={"图片","文字"};
        int[] view={R.id.iv,R.id.tv};
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.simple_adapter_layout,key,view);
        lv.setAdapter(adapter);
    }
    public void initList(){
        list=new ArrayList<>();
        for(int i=1;i<=20;i++) {
            HashMap<String,Object> hashMap=new HashMap<String, Object>();
            hashMap.put("图片",R.drawable.tomcat);
            hashMap.put("文字","数据"+i);
            list.add(hashMap);
        }
    }
}

自己创造的布局文件(只能含有ImageView和Text View)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>
(3)运行结果
simpleAdapter

四、自定义adapter

(1)用法

需要继承BaseAdapter类,重写getCount方法(要展示多少条数据),getItemId方法(返回第几条),getItem方法(获取某一位置的数据对象),getView方法(返回一个View),自定义一个布局文件让自定义的adapter来加载,自定义Adapter的优化,设置一个holder内部类来重用view类。

(2)代码示例

Activity的代码

public class MainActivity extends AppCompatActivity {
ListView lv;
List<User> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
        lv=findViewById(R.id.lv);
        MyAdapter adapter=new MyAdapter(this,list);
        lv.setAdapter(adapter);
    }
    public void initList(){
      list=new ArrayList<>();
      for(int i=1;i<=20;i++){
          User user=new User();
           user.setName("张"+i+"山");
           user.setAge(20+i);
      list.add(user);
      }
    }
}

自定义布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <ImageView
        android:src="@drawable/tomcat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
</LinearLayout>

自定义Javabean类

class User {
    String name;
    int age;
    public void setName(String name){
        this.name=name;
    }
    public void setAge(int age){
        this.age=age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
      return this.age;
    }
}

自定义Adapter

public class MyAdapter extends BaseAdapter {
    Context context;
    List<User> list;
    LayoutInflater inflater;
    public MyAdapter( Context context, List<User> list){
     this.context=context;
     this.list=list;
     inflater= LayoutInflater.from(context);
    }
    public int getCount(){
     return list.size();
    }
    public Object getItem(int position){
        return list.get(position);
    }
    public long getItemId(int position){
        return position;
    }
    public View getView(int position,View converView, ViewGroup parent){
        Holder holder;
        if(converView==null){
            converView=inflater.inflate(R.layout.adapter_layout,null);
            holder=new Holder();
           holder.tv1=converView.findViewById(R.id.tv1);
            holder.tv2=converView.findViewById(R.id.tv2);
            converView.setTag(holder);
        }else{
            holder=(Holder) converView.getTag();
        }
        holder.tv1.setText("名字:"+list.get(position).getName());
        holder.tv2.setText("年龄:"+list.get(position).getAge());
        return converView;
    }
    class Holder{
        TextView tv1;
        TextView tv2;
    }
}
(3)运行结果
自定义Adapter

相关文章

网友评论

    本文标题:Android之ListView

    本文链接:https://www.haomeiwen.com/subject/ajgyuctx.html