Android——BaseAdapter相关
Posted 柒寒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android——BaseAdapter相关相关的知识,希望对你有一定的参考价值。
layout文件:
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.hanqi.testapp2.TestActivity8"> 11 12 <ListView 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:id="@+id/lv_3"></ListView> 16 </LinearLayout>
java类:
1 package com.hanqi.testapp2; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.ImageView; 10 import android.widget.ListView; 11 import android.widget.TextView; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class TestActivity8 extends AppCompatActivity { 17 18 ListView lv_3; 19 List<MyClass> lm; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test8); 24 25 lv_3 = (ListView)findViewById(R.id.lv_3); 26 27 //数据集合 28 lm = new ArrayList<>(); 29 MyClass myClass = new MyClass(R.drawable.f1,"美食1","美食1的介绍"); 30 lm.add(myClass); 31 lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍")); 32 lm.add(new MyClass(R.drawable.f3,"美食3","美食3的介绍")); 33 lm.add(new MyClass(R.drawable.f4,"美食4","美食4的介绍")); 34 lm.add(new MyClass(R.drawable.f5,"美食5","美食5的介绍")); 35 lm.add(new MyClass(R.drawable.f6,"美食6","美食6的介绍")); 36 lm.add(new MyClass(R.drawable.f7,"美食7","美食7的介绍")); 37 lm.add(new MyClass(R.drawable.f8,"美食8","美食8的介绍")); 38 lm.add(new MyClass(R.drawable.f9,"美食9","美食9的介绍")); 39 40 MyBaseAdapter myBaseAdapter = new MyBaseAdapter(); 41 { 42 lv_3.setAdapter(myBaseAdapter); 43 } 44 } 45 46 //装载数据的类 实体类 POJO类 47 //只包含属性和属性的getter和setter方法 48 class MyClass 49 { 50 //img 51 private int img; 52 //name 53 private String name; 54 //content 55 private String content; 56 57 public int getImg() { 58 return img; 59 } 60 61 public void setImg(int img) { 62 this.img = img; 63 } 64 65 public String getName() { 66 return name; 67 } 68 69 public void setName(String name) { 70 this.name = name; 71 } 72 73 public String getContent() { 74 return content; 75 } 76 77 public void setContent(String content) { 78 this.content = content; 79 } 80 //默认构造方法 81 public MyClass() 82 { 83 84 } 85 //直接初始化属性的构造方法 86 public MyClass(int img,String name,String content) 87 { 88 this.img = img; 89 this.name = name; 90 this.content = content; 91 } 92 } 93 94 //内部类继承抽象类BaseAdapter实现4个方法 95 class MyBaseAdapter extends BaseAdapter 96 { 97 @Override 98 public int getCount() { 99 return lm.size(); 100 } 101 102 @Override 103 public Object getItem(int position) { 104 return lm.get(position); 105 } 106 107 @Override 108 public long getItemId(int position) { 109 return 0; 110 } 111 112 //返回适配完成的视图 113 //convertView 可以复用的视图 114 @Override 115 public View getView(int position, View convertView, ViewGroup parent) { 116 117 //适配一条数据和View 118 //1.得到数据 119 MyClass myClass = lm.get(position); 120 121 if (convertView ==null) 122 { 123 Log.e("TAG","适配器getView = "+position); 124 //2.得到 View 125 //加载layout文件 126 convertView = View.inflate(TestActivity8.this, R.layout.simple_adapter, null); 127 } 128 //3.把数据放到View里 129 ImageView imageView = (ImageView)convertView.findViewById(R.id.iv_2); 130 imageView.setImageResource(myClass.getImg()); 131 132 TextView textView = (TextView)convertView.findViewById(R.id.tv_7); 133 textView.setText(myClass.getName()); 134 135 TextView textView1 = (TextView)convertView.findViewById(R.id.tv_8); 136 textView1.setText(myClass.getContent()); 137 return convertView; 138 } 139 } 140 }
效果图为:
以上是关于Android——BaseAdapter相关的主要内容,如果未能解决你的问题,请参考以下文章
如何在android中使用baseadapter刷新自定义列表视图
android -BaseAdapter 不会向 listView 添加新项目