[置顶] Android BaseAdapter

BaseAdapter是Android应用程序中经常用到的基础数据适配器,其主要用途为将一组数据传递到ListView,Spinner,GridView等UI组件。BaseAdapter对于前面提到的三个UI组件来说是万能的,ListView、Spinner、GridView各自都有自己的适配器,但是BaseAdapter对于它们是通用的。比如我们经常使用的ListView的SimpleAdapter是继承自BaseAdapter的。

下面就来看一看BaseAdapter的用法:
使用BaseAdapter时,我们常写一个类来继承它。因为BaseAdapter是一个抽象类,所以要在类中实现它的方法。

public class MyAdapter extends BaseAdapter{
        private Context context;
        private List<String> listBase;

        public MyAdapter(Context context,ArrayList<String> listBase){
            this.context = context;
            this.listBase = listBase;
        }

        @Override
        public int getCount() {
            return listBase.size();
        }

        @Override
        public Object getItem(int i) {
            return listBase.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null){
                view = View.inflate(context,R.layout.list_item_view,null);
                viewHolder = new ViewHolder();
                viewHolder.textView = (TextView) view.findViewById(R.id.textView1);
                view.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.textView.setText(listBase.get(i));
            return view;
        }

        public class ViewHolder{
            public TextView textView;
        }
    }

子项布局文件:
list_item_view.xml

<?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">

    <LinearLayout  android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">

        <TextView  android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

下面是整体代码:
MainActivity.java

public class MainActivity extends Activity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list_view);
        List<String> listData = new ArrayList<String>();
        for (int i =0;i<100;i++){
            listData.add("Item"+i);
        }
        listView.setAdapter(new MyAdapter(this, (ArrayList<String>) listData));
    }

    public class MyAdapter extends BaseAdapter{
        private Context context;
        private List<String> listBase;

        public MyAdapter(Context context,ArrayList<String> listBase){
            this.context = context;
            this.listBase = listBase;
        }

        @Override
        public int getCount() {
            return listBase.size();
        }

        @Override
        public Object getItem(int i) {
            return listBase.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null){
                view = View.inflate(context,R.layout.list_item_view,null);
                viewHolder = new ViewHolder();
                viewHolder.textView = (TextView) view.findViewById(R.id.textView1);
                view.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.textView.setText(listBase.get(i));
            return view;
        }

        public class ViewHolder{
            public TextView textView;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

[置顶] Android BaseAdapter_第1张图片

你可能感兴趣的:(android)