Android开发(11)-利用listview控件显示person表中的所有数据(并实现点击触发拨号程序)

Android开发(11)-利用listview控件显示person表中的所有数据(并实现点击触发拨号程序)

分类: java Android开发 347人阅读 评论(0) 收藏 举报

核心代码如下:

MainActivity.java

  1. package com.example.lession05_dbs;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.AdapterView;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.ListView;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17.   
  18. import com.example.lession05_dbs.dao.PersonDao;  
  19. import com.example.lession05_dbs.dao.PersonDaoImpl;  
  20. import com.example.lession05_dbs.domain.Person;  
  21.   
  22. public class MainActivity extends Activity {  
  23.   
  24.     //listView控件  
  25.     public ListView listView;  
  26.     //显示所有的用户信息  
  27.     public List<Person> persons;  
  28.     //数据库操作的对象  
  29.     public PersonDao personDao ;  
  30.     @Override  
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.activity_main);  
  34.         //实例化数据库操作对象  
  35.         personDao = new PersonDaoImpl(MainActivity.this);  
  36.           
  37.         //执行查询数据  
  38.         persons = personDao.findAll();  
  39.           
  40.         //根据id获取控件对象  
  41.         listView = (ListView) findViewById(R.id.lv_persons);  
  42.         //设置显示的数据 Adpater  
  43.         listView.setAdapter(new PersonListAdapter());  
  44.           
  45.         //listView注册事件  
  46.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  47.   
  48.             /** 
  49.              * parent :listView 
  50.              * view 每个条目控件 
  51.              * position:条目所在的位置 
  52.              * id:行号 0  
  53.              */  
  54.             @Override  
  55.             public void onItemClick(AdapterView<?> parent, View view,  
  56.                     int position, long id) {  
  57.               
  58.                  TextView tv = (TextView) view;  
  59.                    
  60.                 // Toast.makeText(getApplicationContext(), tv.getText().toString(),Toast.LENGTH_LONG).show();  
  61.                   
  62.                  //获取控件的文本  
  63.               /*   String text = tv.getText().toString(); 
  64.                  //拆分 
  65.                  String arr[] =  text.split("-"); 
  66.                  //得到电话 
  67.                  String phone = arr[1]; 
  68.                  //意图 
  69.                  Intent intent = new Intent(); 
  70.                  //设置动作 
  71.                  intent.setAction(Intent.ACTION_CALL); 
  72.                  //设置数据 
  73.                  intent.setData(Uri.parse("tel:"+phone)); 
  74.                  //执行意图 
  75.                  startActivity(intent);*/  
  76.                    
  77.                 //parent listView   
  78.                 Person p =  (Person) parent.getItemAtPosition(position); //返回值 实际就是getItem返回的值  
  79.                /* //意图 
  80.                  Intent intent = new Intent(); 
  81.                  //设置动作 
  82.                  intent.setAction(Intent.ACTION_CALL); 
  83.                  //设置数据 
  84.                  intent.setData(Uri.parse("tel:"+p.getPhone())); 
  85.                  //执行意图 
  86.                  startActivity(intent); 
  87.                  */  
  88.                 // Toast.makeText(getApplicationContext(), p.getName(), Toast.LENGTH_LONG).show();  
  89.                   
  90.                int lid =  (int) parent.getItemIdAtPosition(position);  
  91.                  
  92.                //   
  93.                Toast.makeText(getApplicationContext(), lid+"", Toast.LENGTH_LONG).show();  
  94.                    
  95.                    
  96.                    
  97.             }  
  98.         });  
  99.     }  
  100.   
  101.     @Override  
  102.     public boolean onCreateOptionsMenu(Menu menu) {  
  103.         // Inflate the menu; this adds items to the action bar if it is present.  
  104.         getMenuInflater().inflate(R.menu.main, menu);  
  105.         return true;  
  106.     }  
  107.   
  108.       
  109.     class PersonListAdapter extends BaseAdapter{  
  110.   
  111.         //告诉你有多少个条目  
  112.         @Override  
  113.         public int getCount() {  
  114.             // TODO Auto-generated method stub  
  115.             return persons.size();  
  116.         }  
  117.   
  118.         //返回这个为控件对应的数据  
  119.         @Override  
  120.         public Object getItem(int position) {  
  121.             // TODO Auto-generated method stub  
  122.             return persons.get(position);  
  123.         }  
  124.   
  125.         //返回条目所在的位置  
  126.         @Override  
  127.         public long getItemId(int position) {  
  128.             // TODO Auto-generated method stub  
  129.             return position;  
  130.         }  
  131.   
  132.         @Override  
  133.         public View getView(int position, View convertView, ViewGroup parent) {  
  134.             //创建一个显示的控件  每个条目对应的控件  
  135.             TextView tv = new TextView(MainActivity.this);  
  136.             tv.setText(persons.get(position).getName()+"-"+persons.get(position).getPhone());  
  137.             return tv;  
  138.         }  
  139.           
  140.     }  
  141. }  
注意在清单文件中添加 拨号权限:

 <uses-permission android:name="android.permission.CALL_PHONE"/>

其他实体类,接口 和实现类,参见上篇博文

点击打开链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值