2011年5月31日星期二

  四种Adapter(ArrayAdapte,SimpleAdapterr,SimpleCursorAdapter和BaseAdapter)的实现

这里说的4种指的ArrayAdapte,SimpleAdapterr,SimpleCursorAdapter和BaseAdapter,具体会用哪种,主要是看你的和集合数据关系的View(List,GridView等)能set哪种Adapter,及你的集合数据能怎么获取。

这里就不在赘述。
主要看他们具体的实现
1.ArrayAdapter
//可以定义复杂的布局,每个Item中可以是ImageView,TextView等
public class ToDoItemAdapter extends ArrayAdapter {
        int resource;
        //可以用对象数组和对象list
        public ToDoItemAdapter(Context _context,
                int _resource,
                List _items) {
        super(_context, _resource, _items);
        resource = _resource;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                LinearLayout todoView;
                ToDoItem item = getItem(position);
                String taskString = item.getTask();
                Date createdDate = item.getCreated();
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
                String dateString = sdf.format(createdDate);
               
                if (convertView == null) {
                        todoView = new LinearLayout(getContext());
                        String inflater = Context.LAYOUT_INFLATER_SERVICE;
                        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
                        vi.inflate(resource, todoView, true);
                } else {
                        todoView = (LinearLayout) convertView;
                }
               
                TextView dateView = (TextView)todoView.findViewById(R.id.rowDate);
                TextView taskView = (TextView)todoView.findViewById(R.id.row);
                dateView.setText(dateString);
                taskView.setText(taskString);
                return todoView;
        }
}
private ToDoItemAdapter aa = new ToDoItemAdapter(this, resID, todoItems);

2.SimpleAdapter

SimpleAdapter menuAdapter = new SimpleAdapter(
                        this,
                        getGridData(),//返回的是个放s了map的list
                        R.layout.grid_menu,
                        new String[]{"menuImage","menuName"},
                        new int[]{R.id.menu_item_image,R.id.menu_item_name}
                );
mainGrid.setAdapter(menuAdapter);

3.SimpleCursorAdapter

String uriString = "content://contacts/people/";
Cursor myCursor = managedQuery(Uri.parse(uriString), null, null, null);
String[] fromColumns = new String[] {People.NUMBER, People.NAME};
int[] toLayoutIDs = new int[] { R.id.nameTextView, R.id.numberTextView};
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this,
                R.layout.simplecursorlayout,
                myCursor,
                fromColumns,
                toLayoutIDs);
myListView.setAdapter(myAdapter);

4.BaseAdapter的实现

/*
* BaseAdapter的实现
* //它是ListAdapter和SpinnerAdapter子类,GridView要用SpinnerAdapter,所以实现BaseAdapter能让GridView使用
*/

public class ImageAdapter extends BaseAdapter{
       
        Context context;
       
        public ImageAdapter(Context c){
                context = c;
        }
       
        public int getCount() {
                return filePaths.length;
        }

        //这个方法其实没什么用,可以随意返回
        public Object getItem(int position) {
                return filePaths[position];
        }

        //这个方法其实也没什么用,可以随意返回
        public long getItemId(int position) {
                return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
                ImageView iv = new ImageView(context);
               
               
                ImageSwitchActivity.this.filePath = filePaths[position];
               
                distoryBitmap();
                //currBitmap = BitmapFactory.decodeFile(filePaths[position]);
               
                iv.setImageDrawable(Drawable.createFromPath(filePaths[position]));
                //is.setImageDrawable(ifResizeFromBitMap(currBitmap));
               
                iv.setAdjustViewBounds(true);
                iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                //iv.setBackgroundResource(R.drawable.e);
                return iv;
        }
       
}

没有评论:

发表评论