上面所述的adapter只不过是通过观看源代码的形式,将其实现出来,用法还不如它原来的两个类,arrayAdapter和simpleAdapter好用,通过以下的修改可以发现,有了很大的改观,
xml文件中和自己创建的xml文件没有进行多大的修改,改的最大的地方就是对于数据的封装,将数据封装到对象中,直接通过对象来传值这样鲜果会更好一点。
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView> </RelativeLayout>
自己创建的xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearlayout_id1" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearlayout_id2" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
对于自己继承的那个类,做了如下修改
package com.will.testt; import java.util.HashMap; import java.util.List; import java.util.Map; import cn.core.entity.Students; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MyBaseAdapter extends BaseAdapter { private Context mContext; private List <Students> mData; public MyBaseAdapter(Context mContext, List<Students> data) { this.mContext = mContext; this.mData = data; } @Override public int getCount() { // TODO Auto-generated method stub return mData.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) {
//将数据封装到对象中 Students stu=mData.get(position); //xml解析器 LayoutInflater layoutInflater = LayoutInflater.from(mContext);
//所有的什么布局都继承自view,因此利用view会更好用 View view=(View)layoutInflater.inflate(R.layout.activity_main_lv, null); //通过id找到里面的组件 ImageView imageView=(ImageView)view.findViewById(R.id.imageView1); TextView textView1=(TextView)view.findViewById(R.id.textView1); TextView textView2=(TextView)view.findViewById(R.id.textView2);
//将对象中的数据赋值给组件中的文本即可 imageView.setImageResource(stu.stuImageId); textView1.setText(stu.stuName); textView2.setText(stu.stuPhone);
//返回该view return view; } }
package com.will.test; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListAdapter; import android.widget.ListView; import cn.core.entity.Students; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Students stu1=new Students(1, R.drawable.ic_launcher, "tom1", "110"); Students stu2=new Students(2, R.drawable.ic_launcher, "tom2", "114"); Students stu3=new Students(3, R.drawable.ic_launcher, "tom4", "110"); Students stu4=new Students(4, R.drawable.ic_launcher, "tom5", "114"); Students stu5=new Students(5, R.drawable.ic_launcher, "tom6", "110"); Students stu6=new Students(6, R.drawable.ic_launcher, "tom7", "114"); List<Students> data=new ArrayList<Students>(); data.add(stu1); data.add(stu2); data.add(stu3); data.add(stu4); data.add(stu5); data.add(stu6); ListView listView=(ListView)findViewById(R.id.listView1); ListAdapter listAdapter = new MyBaseAdapter(this,data); listView.setAdapter(listAdapter); } }
使用自己编写的类名,然后利用该方法,传入两个值,一个时this代替的context,一个就是list数组这样就可以实现自己adapter中的扩展,这种方法在实际开发当中是相当重要的。