MyAdapter自定义适配器:
//具体方法如下:
-------------------------------------------------------------------------------------------
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------>建一个<activity (这里写入android:name="com.eduask.dialing.类名")></activity>标签)
在写代码时常范的错误:用LinearLayout时要记得给<LinearLayout设置宽度和高度></LinearLayout>不然将无法显示写在LinearLayout中的内容!切记-。-、、!!!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。