MyAdapter自定義適配器:
創(chuàng)新互聯(lián)自2013年起,先為合江等服務(wù)建站,合江等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為合江企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
//具體方法如下:
-------------------------------------------------------------------------------------------
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);
// 加載相應(yīng)數(shù)據(jù)
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);
//添加數(shù)據(jù)
intent.setData(Uri.parse("tel:"+phones[position]));
startActivity(intent);
}
});
-------------------------------------------------------------------------------------------
短信功能(頁面跳轉(zhuǎn))
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;
}
});
-------------------------------------------------------------------------------------------
在用模擬器實現(xiàn)打電話發(fā)送短信等功能時需要在運行給它授予權(quán)限(授權(quán)方法:AndtoidManifest------>Permissions----->Add---->Uses Permission----->選擇要授予的權(quán)限---->Ctrl+s),若想實現(xiàn)發(fā)短信功能還需要先進(jìn)行注冊(注冊方法:AndtoidManifest------>AndroidManifesf.xml------>建一個
在寫代碼時常范的錯誤:用LinearLayout時要記得給
本文名稱:自定義適配器與電話功能
路徑分享:http://weahome.cn/article/jpdejs.html