标签:
前段时间写了个通讯录,现在闲的正好回顾回顾。
其实对我的感触是
一:实现方式有很多,那就要重于积累。但难免有落后的,设计不合理的。
二:还有就是设计方面,整体的设计上。编程不就是逻辑嘛。参考别人的,形成自己的。
1.显示效果,很简单,直接Listview
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingLeft="@dimen/activity_horizontal_margin" 6 android:paddingRight="@dimen/activity_horizontal_margin" 7 android:paddingTop="@dimen/activity_vertical_margin" 8 android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 9 10 11 <ListView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:id="@+id/lv" 15 android:layout_alignParentTop="true" 16 android:layout_alignParentLeft="true" 17 android:layout_alignParentStart="true" /> 18 </RelativeLayout>
2.获取Liseview里所需元素,即获取通讯录信息。(从数据库中取出数据封装成对象,完完全全可以写个工具类)
1 public class GetPhoneNumber { 2 3 //ToDo 解释这里lists,PhoneInfo的作用 4 public static List<PhoneInfo> lists = new ArrayList<PhoneInfo>(); 5 6 public static String getPhoneNumber(Context context) { 7 8 Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null); 9 10 String phoneNumber; 11 String phoneName; 12 while (cursor.moveToNext()) { 13 phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 14 phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 15 PhoneInfo phoneInfo = new PhoneInfo(phoneName,phoneNumber); 16 lists.add(phoneInfo); 17 System.out.println(phoneName + ":" + phoneNumber); 18 } 19 return null; 20 } 21 }
获取方法如上,通过ArrayList将数据封装。以后可以根据需求再增加。
3.自定义PhoneInfo类型
1 public class PhoneInfo { 2 3 private String phoneName; 4 private String phoneNumber; 5 6 //ToDo get,set之后的作用 7 public PhoneInfo(String phoneName,String phoneNumber){ 8 setPhoneName(phoneName); 9 setPhoneNumber(phoneNumber); 10 } 11 12 13 public String getPhoneName() { 14 return phoneName; 15 } 16 17 public void setPhoneName(String phoneName) { 18 this.phoneName = phoneName; 19 } 20 21 public String getPhoneNumber() { 22 return phoneNumber; 23 } 24 25 public void setPhoneNumber(String phoneNumber) { 26 this.phoneNumber = phoneNumber; 27 } 28 }
第一次遇见初始化这样的写法,有趣。
4.自定义BaseAdapter
1 //ToDo 感受适配器,内容,形式之间的巧妙联系 2 public class MyAdapter extends BaseAdapter{ 3 4 private List<PhoneInfo> lists;//内容 5 private Context context; 6 // private LinearLayout layout; 7 8 public MyAdapter(List<PhoneInfo> lists,Context context) { 9 this.lists = lists; 10 this.context = context; 11 } 12 13 @Override 14 //返回的是集合的数量 15 public int getCount() { 16 return lists.size(); 17 } 18 19 @Override 20 //返回的是当前的数据 21 public Object getItem(int position) { 22 return lists.get(position); 23 } 24 25 @Override 26 //获取当前的ID 27 public long getItemId(int position) { 28 return position; 29 } 30 31 @Override 32 //获取到当前要加载的View,同时将内容加载到视图当中 33 public View getView(int position, View convertView, ViewGroup parent) { 34 // LayoutInflater inflater = LayoutInflater.from(context); 35 // layout = (LinearLayout) inflater.inflate(R.layout.call,null); 36 // TextView nametv = (TextView) layout.findViewById(R.id.name); 37 // TextView numbertv = (TextView) layout.findViewById(R.id.number); 38 // nametv.setText(lists.get(position).getPhoneName()); 39 // numbertv.setText(lists.get(position).getPhoneNumber()); 40 //优化操作 41 42 ViewHolder holder; 43 if(convertView == null){ 44 convertView = LayoutInflater.from(context).inflate(R.layout.call,null); 45 holder = new ViewHolder(); 46 holder.nametv = (TextView) convertView.findViewById(R.id.name); 47 holder.numbertv = (TextView) convertView.findViewById(R.id.number); 48 holder.nametv.setText(lists.get(position).getPhoneName()); 49 holder.numbertv.setText(lists.get(position).getPhoneNumber()); 50 convertView.setTag(holder); 51 52 }else { 53 holder = (ViewHolder)convertView.getTag(); 54 holder.nametv.setText(lists.get(position).getPhoneName()); 55 holder.numbertv.setText(lists.get(position).getPhoneNumber()); 56 } 57 58 return convertView; 59 } 60 61 public static class ViewHolder{ 62 TextView nametv; 63 TextView numbertv; 64 } 65 66 }
5.内容表现形式
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal"> 6 <LinearLayout 7 android:orientation="horizontal" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content"> 10 <ImageView 11 android:layout_width="64dp" 12 android:layout_height="62dp" 13 android:id="@+id/imageView" 14 android:src="@drawable/abc_ic_clear_mtrl_alpha" /> 15 <LinearLayout 16 android:orientation="vertical" 17 android:layout_width="match_parent" 18 android:layout_height="match_parent" 19 android:gravity="center_vertical"> 20 <TextView 21 android:id="@+id/name" 22 android:textSize="25sp" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="Name"/> 26 <TextView 27 android:id="@+id/number" 28 android:textSize="17sp" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="Number"/> 32 </LinearLayout> 33 </LinearLayout> 34 </LinearLayout>
6.MainActivity.java
1 public class MainActivity extends ActionBarActivity { 2 3 private ListView lv; 4 private MyAdapter adapter; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 GetPhoneNumber.getPhoneNumber(this); 11 lv = (ListView) findViewById(R.id.lv); 12 13 adapter = new MyAdapter(GetPhoneNumber.lists,this); 14 lv.setAdapter(adapter); 15 } 16 17 @Override 18 //解决数据重复加载的问题 19 public void onBackPressed() { 20 GetPhoneNumber.lists.clear(); 21 super.onBackPressed(); 22 } 23 }
请允许给我段时间感受这样写的好处。
待续………
似乎有不错的工具类:
http://mjbb.iteye.com/blog/810011#bc1791262
http://mjbb.iteye.com/blog/810017#bc1791265
标签:
原文地址:http://www.cnblogs.com/ryan-ys/p/4686313.html