首页 > 代码库 > Android通用Adapter使用例子

Android通用Adapter使用例子

Android中Adapter如果继承自BaseAdapter的话必须实现一下四个方法 

getCount(); getItem(int position);
getItemId(int position);
getView(int position, View convertView, ViewGroup parent);

其实每个adapter真正变化的部分就是getView方法。这里用于控制每个Item显示出来的效果。 所以我自定义了一个BaseAdapter,叫做AppBaseAdapter。代码如下:

/**
     * Adapter基类
     */
    public abstract class AppBaseAdapter<T> extends BaseAdapter {

        public List<T> list = new ArrayList<T>();
        public LayoutInflater inflater;
        public Context mContext;
        public AppApplication appContext;

        public AppBaseAdapter(Context context) {
            this.mContext = context;
            appContext = AppApplication.getInstance();
            inflater = LayoutInflater.from(mContext);
        }

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

        @Override
        public T getItem(int position) {
            return list.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return convertView;
        }
        //向list中添加数据
        public void refreshAdapter(List<T> arrayList) {
            list.addAll(arrayList);
            notifyDataSetChanged();
        }
        //清空list集合
        public void clear() {
            list.clear();
            notifyDataSetChanged();
        }
        //移除指定位置的对象
        public void remove(int index) {
            list.remove(index);
            notifyDataSetChanged();
        }
        //移除对象
        public void remove(Object obj) {
            list.remove(obj);
            notifyDataSetChanged();
        }

    }
使用一个简单的泛型,暂时不去考虑list的数据类型。AppBaseAdapter一个好处就是可以实现数据的延迟绑定。假设你要先从服务器获取数据,然后通过listview显示,使用AppBaseAdapter之后 可以先setAdatper,等到数据加载完成之后,通过调用refreshAdapter(list集合)即可显示数据
下面给出一个简单的使用例子:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MyActivity extends Activity {

    private ListView mListView;
    private View mEmptyView;
    private MyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListView = (ListView) this.findViewById(R.id.listview);
        mEmptyView = this.findViewById(R.id.empty);

        adapter = new MyAdapter(this);
        mListView.setAdapter(adapter);
        //setEmptyView中传入的view一定要包含在根view中
        mListView.setEmptyView(mEmptyView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        new Thread() {
            @Override
            public void run() {
                super.run();

                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                final ArrayList<String> strings = new ArrayList<String>();
                for (int i = 0, count = 10; i < count; i++) {
                    strings.add("count is :" + i);
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.refreshAdapter(strings);
                    }
                });

            }
        }.start();
    }

    class MyAdapter extends AppBaseAdapter {
        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.list_item, null);
                holder.textView = (TextView) convertView.findViewById(R.id.textview);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String item = (String) list.get(position);
            holder.textView.setText(item);
            return convertView;
        }

        class ViewHolder {
            TextView textView;
        }
    }
}
deme下载地址: http://www.oschina.net/code/snippet_924286_44753   

Android通用Adapter使用例子