我是个新手,为了加深对ListView和BaseAdapter的理解,写了这个博客。
1.在activity_main.xml中添加ListView,代码如下:
<?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" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_view"/> </LinearLayout
2.在layout目录下创建fruit.item.xml,自己可以的设计你喜欢的界面,这里我弄简单点,
<?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"> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/fruit_image" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fruit_name" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" /> </LinearLayout>
3.下一步创建加载数据的Fruit类了,在com.example.administrator.(..)下,右键点new-java class 新建Fruit类
代码:
package com.example.administrator.listviewtest; //你设计多少组件,你就声明多少个对象 //这也是你链表(LinkedList<>)的格式 public class Fruit { private String aName; private int aImageId; public Fruit(){ } public Fruit(String aName,int aImageId){ this.aName=aName; this.aImageId=aImageId; } public String getaName(){ return aName; } public int getaImageId() { return aImageId; } public void setaName(String aName){ this.aName=aName; } public void setaImageId(int aImageId){ this.aImageId=aImageId; } }
4.这一步就要创建适配了,我用了最常用的BaseAdapter适配器,其实精通这一个就好了。
package com.example.administrator.listviewtest; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.LinkedList; import java.util.List; public class FruitAdapter extends BaseAdapter { //声明一个链表和Context对象 private LinkedList<Fruit> mList; private Context mContext; public FruitAdapter(LinkedList<Fruit> mList,Context mContext){ this.mList=mList; this.mContext=mContext; } @Override //获取数据的数量 public int getCount(){ return mList.size(); } @Override //获取数据的内容 public Object getItem(int position){ return null; } @Override //获取数据的id public long getItemId(int position){ return position; } @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //加载一个适配器界面 convertView=LayoutInflater.from(mContext).inflate(R.layout.fruit_item,parent,false); //实例化元件 ImageView image=(ImageView) convertView.findViewById(R.id.fruit_image); TextView text=(TextView) convertView.findViewById(R.id.fruit_name); //元件获取数据 image.setImageResource(mList.get(position).getaImageId()); text.setText(mList.get(position).getaName()); return convertView; } }
5.最后只要配置一下MainActivity.java就可以了
package com.example.administrator.listviewtest; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class MainActivity extends AppCompatActivity { private LinkedList<Fruit> mList=null; private ListView listView; private Context mContext; private FruitAdapter adapter=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext=MainActivity.this; listView=(ListView) findViewById(R.id.list_view); mList = new LinkedList<Fruit>();//实例化LinkedList mList.add(new Fruit("葡萄",R.drawable.fruit1));//增加数据到链表 mList.add(new Fruit("西瓜",R.drawable.fruit2)); mList.add(new Fruit("杏子",R.drawable.fruit3)); adapter=new FruitAdapter((LinkedList<Fruit>)mList,mContext);//设置适配器 listView.setAdapter(adapter);//ListView设置适配器 } }
6.效果如下
扫描二维码关注公众号,回复:
1680890 查看本文章
![](/qrcode.jpg)