View(视图)——ListView之BaseAdapter

Posted 安然罒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了View(视图)——ListView之BaseAdapter相关的知识,希望对你有一定的参考价值。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.testapp2.TestActivity9">
11 
12     <ListView
13         android:layout_width="match_parent"
14         android:layout_height="match_parent"
15         android:id="@+id/lv_3">
16 
17     </ListView>
18 
19 </LinearLayout>
.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <ImageView
 7         android:layout_width="70dp"
 8         android:layout_height="70dp"
 9         android:src="@drawable/f1"
10         android:id="@+id/iv_2"/>
11     <LinearLayout
12         android:layout_width="0dp"
13         android:layout_height="match_parent"
14         android:layout_weight="1"
15         android:orientation="vertical"
16         android:layout_marginLeft="20dp"
17         android:gravity="center_vertical">
18 
19         <TextView
20             android:layout_width="match_parent"
21             android:layout_height="wrap_content"
22             android:text="名字=aaa"
23             android:id="@+id/tv_7"/>
24 
25         <TextView
26             android:layout_width="match_parent"
27             android:layout_height="wrap_content"
28             android:text="内容=aaa"
29             android:id="@+id/tv_8"/>
30 
31     </LinearLayout>
32 
33 </LinearLayout>
simple_adapter.xml
  1 package com.example.wang.testapp2;
  2 
  3 import android.support.v7.app.AppCompatActivity;
  4 import android.os.Bundle;
  5 import android.util.Log;
  6 import android.view.LayoutInflater;
  7 import android.view.View;
  8 import android.view.ViewGroup;
  9 import android.widget.BaseAdapter;
 10 import android.widget.ImageView;
 11 import android.widget.ListView;
 12 import android.widget.TextView;
 13 
 14 import java.util.ArrayList;
 15 import java.util.List;
 16 
 17 public class TestActivity9 extends AppCompatActivity {
 18 
 19     ListView lv_3;
 20 
 21     List<MyClass> lm;
 22 
 23     @Override
 24     protected void onCreate(Bundle savedInstanceState) {
 25         super.onCreate(savedInstanceState);
 26         setContentView(R.layout.activity_test9);
 27 
 28         lv_3=(ListView)findViewById(R.id.lv_3);
 29 
 30         //数据集合
 31         lm=new ArrayList<>();
 32 
 33         MyClass myClass=new MyClass(R.drawable.f1,"美食1","美食1的介绍");
 34 
 35         lm.add(myClass);
 36 
 37         lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
 38         lm.add(new MyClass(R.drawable.f3,"美食3","美食3的介绍"));
 39         lm.add(new MyClass(R.drawable.f4,"美食4","美食4的介绍"));
 40         lm.add(new MyClass(R.drawable.f5,"美食5","美食5的介绍"));
 41         lm.add(new MyClass(R.drawable.f6,"美食6","美食6的介绍"));
 42         lm.add(new MyClass(R.drawable.f7,"美食7","美食7的介绍"));
 43         lm.add(new MyClass(R.drawable.f8,"美食8","美食8的介绍"));
 44         lm.add(new MyClass(R.drawable.f9,"美食9","美食9的介绍"));
 45         lm.add(new MyClass(R.drawable.f10,"美食10","美食10的介绍"));
 46 
 47         MyBaseAdapter myBaseAdapter=new MyBaseAdapter();
 48 
 49         lv_3.setAdapter(myBaseAdapter);
 50 
 51     }
 52 
 53     //装载数据的类  实体类  POJO类:简单Java对象
 54     //一般只包含属性和属性的getter和setter方法
 55     class MyClass
 56     {
 57         //img
 58         private int img;
 59 
 60         //name
 61         private String name;
 62 
 63         //content
 64         private String content;
 65 
 66         public int getImg() {
 67             return img;
 68         }
 69 
 70         public void setImg(int img) {
 71             this.img = img;
 72         }
 73 
 74         public String getName() {
 75             return name;
 76         }
 77 
 78         public void setName(String name) {
 79             this.name = name;
 80         }
 81 
 82         public String getContent() {
 83             return content;
 84         }
 85 
 86         public void setContent(String content) {
 87             this.content = content;
 88         }
 89 
 90         //默认构造方法
 91         public MyClass()
 92         {
 93 
 94         }
 95 
 96         //直接初始化属性的构造方法
 97         public MyClass(int img,String name,String content)
 98         {
 99             this.img=img;
100             this.name=name;
101             this.content=content;
102         }
103     }
104 
105     //内部类继承抽象类BaseAdapter 实现4个方法
106 
107     class MyBaseAdapter extends BaseAdapter
108     {
109         @Override
110         public int getCount() {
111             return lm.size();
112         }
113 
114         @Override
115         public Object getItem(int position) {
116             return lm.get(position);
117         }
118 
119         @Override
120         public long getItemId(int position) {
121             return 0;
122         }
123 
124         //返回适配完成的视图
125         //convertView 可以复用的视图
126         @Override
127         public View getView(int position, View convertView, ViewGroup parent) {
128 
129             Log.e("TAG","适配器getView="+position);
130 
131             //适配一条数据和View
132             //1-得到数据
133 
134             MyClass myClass=lm.get(position);
135 
136             if(convertView==null ) {
137 
138 
139                 //2-得到 View
140                 //加载layout文件
141                 convertView = View.inflate(TestActivity9.this, R.layout.simple_adapter, null);
142             }
143 
144             //3-把数据放到View里
145 
146             ImageView imageView= (ImageView)convertView.findViewById(R.id.iv_2);
147 
148             imageView.setImageResource(myClass.getImg());
149 
150             TextView textView=(TextView)convertView.findViewById(R.id.tv_7);
151 
152             textView.setText(myClass.getName());
153 
154             TextView textView1=(TextView)convertView.findViewById(R.id.tv_8);
155 
156             textView1.setText(myClass.getContent());
157 
158             return convertView;
159         }
160     }
161 }
.java

 

以上是关于View(视图)——ListView之BaseAdapter的主要内容,如果未能解决你的问题,请参考以下文章

View(视图)——ListView之ArrayAdapter

View(视图)——ListView之BaseAdapter

View(视图)——ListView之BaseAdapter和SimpleCursorAdapter以及网格视图代码视图

View(视图)——ListView之ArrayAdapter和SimpleAdapter文集

View(视图)——ListView概述Adapter分类

ListView控件有几种视图?通过哪个属性设置?