MyAdapter自定义适配器:
创新互联自2013年起,先为合江等服务建站,合江等地企业,进行企业商务咨询服务。为合江企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
//具体方法如下:
-------------------------------------------------------------------------------------------
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1,找到控件
lv = (ListView) findViewById(R.id.lv_list);
// 2,定义适配器
// >>>>>>>>>>>>>>>>>>>2.3自定义适配器
MyAdapter adapter = new MyAdapter();
// 3,加载适配器
lv.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return objects.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 加载布局 activity加载布局
/*
* LayoutInflater inflater=getLayoutInflater();
* inflater.inflate(resource, root);
*/
View contentView = View.inflate(getApplicationContext(),
R.layout.list_item, null);
// 找到布局里面的控件
ImageView iv = (ImageView) contentView.findViewById(R.id.iv_logo);
TextView tv = (TextView) contentView.findViewById(R.id.tv_name);
// 加载相应数据
iv.setImageResource(imgs[position]);
tv.setText(objects[position]);
return contentView;
}
@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;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------
拨号功能
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
//添加意图
Intent intent = new Intent();
//添加动作
intent.setAction(intent.ACTION_CALL);
//添加数据
intent.setData(Uri.parse("tel:"+phones[position]));
startActivity(intent);
}
});
-------------------------------------------------------------------------------------------
短信功能(页面跳转)
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> parent, View view,
int position, long id) {
Intent i=new Intent(getApplicationContext(), SmsActivity.class);
i.putExtra("number", phones[position]);
startActivity(i);
return true;
}
});
-------------------------------------------------------------------------------------------
在用模拟器实现打电话发送短信等功能时需要在运行给它授予权限(授权方法:AndtoidManifest------>Permissions----->Add---->Uses Permission----->选择要授予的权限---->Ctrl+s),若想实现发短信功能还需要先进行注册(注册方法:AndtoidManifest------>AndroidManifesf.xml------>建一个
在写代码时常范的错误:用LinearLayout时要记得给
网站题目:自定义适配器与电话功能
文章出自:http://lswzjz.com/article/jpdejs.html