android-适配Adapter

时间:2023-03-08 16:58:16

Adapter是把数据和用户界面视图绑定到一起的桥梁类,负责创建用来表示父视图中的每一个条目的子视图,并提供对底层数据的访问。

public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ListView toDoListView = (ListView)findViewById(R.id.toDolistView); final ArrayList<String> todoItems = new ArrayList<String>();
// final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); int resID = R.layout.todolist_item;
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, resID, todoItems); //将adapter绑定到listView
toDoListView.setAdapter(adapter);
}
}
//若是复杂对象的数组,数据的显示需要定制的话则重写adapter的geView方法
abstract public class MyListAdapter<T> extends BaseAdapter { private int listCellId; public MyListAdapter(Context context, int resId, List<T> items) {
super(context, resId, items);
listCellId = resId;
} @Override
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(listCellId, null);
} ((TextView)(convertView)).setText((String)getItem(position)); return convertView;
} }

iOS的tableView使用

@implementation ContactsViewController

- (id)init
{
if (self = [super init]) { contactTable = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
contactTable.frame = CGRectMake(, , self.view.bounds.size.width, self.view.bounds.size.height);
contactTable.delegate = self;
contactTable.dataSource = self;
[self.view addSubview:contactTable];
}
return self;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
} cell.textLabel.text = [[_contactList objectAtIndex:indexPath.row] objectForKey:PersonName];
cell.detailTextLabel.text = [[_contactList objectAtIndex:indexPath.row] objectForKey:PersonTel]; return cell;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%d",_contactList.count);
return _contactList.count;
}

iOS与Android的table使用区别:

iOS在viewController中定义一个数组成员变量,将viewController设置为tableView的dataSource对象,然后在cellForRowAtIndexPath中创建或复用UI,并将数组成员变量根据行位置读取相应数据后将其更新到UI。

Android在activity中定义一个数组变量存放数据,利用该数组、activity的context、cell(子视图)的样式(xml)资源创建一个adapter,将该adapter设置为listView的adapter属性,然后在adapter的getView方法中根据样式资源ID创建UI,并利用position和getItem方法将底层数组对应行的对象读出并更新到UI上。

Adapter就相当于tableViewController,listView或tableView的一些关键回调都在它们中实现,但一些tableViewController中集成的viewController方法,在Adapter中则没有集成上Activity的方法,还是由listView所在的Activity中完成(Adapter和Activity本身类都不一致)。

即Adapter只完成和listView有关的回调。

SimpleCursorAdapter

SimpleCursorAdapter是通过传入当前上下文、一个布局资源、一个Cursor和两个数组进行构建的,这两个数组一个包含了要使用的列的名称,一个存储了用了显示相应列的数据值的视图资源ID。Cursor是指每行的集合。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;
myAdapter = new SimpleCursorAdapter(this, R.layout.simplecursorlayout, myCursor, fromColumns, toLayoutIDs);
myListView.setAdapter(myAdapter);