小写转大写 : ctrl+shift+F
<ScrollView></ScrollView>滚动条显示视图
ListView与BaseAdapter:
1 public class MainActivity extends Activity { 2 private List<Person> pList; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 ListView mlistview=(ListView)findViewById(R.id.listview); 9 //把view与适配器关联 10 mlistview.setAdapter(new MyAdapter()); 11 } 12 13 class MyAdapter extends BaseAdapter{ 14 15 //获取view数据的长度 16 @Override 17 public int getCount() { 18 return pList.size(); 19 } 20 21 @Override 22 public Object getItem(int position) { 23 // TODO Auto-generated method stub 24 return null; 25 } 26 27 @Override 28 public long getItemId(int position) { 29 // TODO Auto-generated method stub 30 return 0; 31 } 32 33 //获得ListView中的某一行的view对象 34 //postion返回当前索引的位置 35 //convertView 缓存对象 36 //parent是listView对象 37 @Override 38 public View getView(int position, View convertView, ViewGroup parent) { 39 TextView tv=null; 40 if(convertView!=null){//判断缓存对象是否为空 41 tv=(TextView)convertView; 42 }else{//等于null程序刚加载,则创建 43 tv=new TextView(MainActivity.this);//类名.this ==上下文 44 } 45 Person p=pList.get(position);//获得指定位置的数据,进行对TextView绑定 46 tv.setText(p.toString()); 47 return tv; 48 49 } 50 51 public View getView2(int position, View convertView, ViewGroup parent){ 52 View view=null; 53 54 if(convertView==null){ 55 //布局填充器对象,用于把xml布局转换成view对象 56 LayoutInflater inflator= MainActivity2.this.getLayoutInflater(); 57 view=inflator.inflate(R.layout.listview2_item, null); 58 }else{ 59 view=convertView; 60 } 61 62 //赋值 63 //。。。 64 return view; 65 } 66 } 67 }
ArrayAdapter与simpleAdapter:
1 public class MainActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 ListView mlistview=(ListView)findViewById(R.id.list_view); 9 10 // ArrayAdapter适配器实例 11 String[] textArr={"a","b","c","d","e","f","g","h","i","t","z"}; 12 //定义数据适配器 13 //用于显示简单的文本内容 14 //android.R.layout.simple_list_item_1 listview的子条目显示布局的id 15 //textArr 显示在list列表中的数据 16 ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, 17 android.R.layout.simple_list_item_1,textArr); 18 mlistview.setAdapter(adapter); 19 20 21 //context 上下文 22 //data List<Map<String,Object>> data 23 //resource listview子条目布局的id 24 //from data数据中的map集合里的key 25 //to resource中的id 26 //SimpleAdapter adp=new SimpleAdapter(context,data,resource,from,to); 27 List<Map<String,Object>> data=new ArrayList<Map<String,Object>>(); 28 Map<String,Object> map=new HashMap<String,Object>(); 29 map.put("name", "zhangsan1"); 30 map.put("age", "17"); 31 SimpleAdapter adp=new SimpleAdapter(this,data,R.layout.listview_item, 32 new String[]{"name","age"},new int[]{R.id.tv_name,R.id.tv_ico}); 33 mlistview.setAdapter(adp); 34 } 35 36 }
ContentProvider:内容提供者抽象类
因为在Android系统里面,数据库是私有的。一般情况下外部应用程序是没有权限读取其它应用程序的数据。如果想公开你自己的数据,有两个选择:可以创建你自己的内容提供器(一个ContentProvider子类)或者你可以给已有的提供器添加数据-如果存在一个控制同样类型数据的内容提供器且你拥有写的权限。而外界根本看不到,也不用看到这个应用暴露的数据在应用当中是如何存储的,或者是用数据库存储还是用文件存储,还是通过网上获得,这些一切都不重要,重要的是外界可以通过这一套标准及统一的接口和程序里的数据打交道,可以读取程序的数据,也可以删除程序的数据,当然,中间也会涉及一些权限的问题。
实现ContentProvider抽象类需要在配置文件中声明:
//并赋读写权限
<provider android:name=".provider.PersonContentProvider"
android:authorities="包名+类名" android:readPermission="随便.read"
android:writePermission="随便2.write" >
</provider>
声明权限还需要在manifest节点下添加:
<manifest>
<permission android:name="随便.read"></permission>
<permission android:name="随便2.write"></permission>
</manifest>
并在对应访问的工程的Manifest.xml中注册(不然无法访问被拒绝):
<uses permission android:name="随便.read" />
<uses permission android:name="随便2.write" />
1 public class PersonContentProvider extends ContentProvider{ 2 private static final String AUTHORITY="包名+类名"; 3 private static final int PERSON_INSERT_CODE=0; //操作person表添加的操作uri匹配码 4 private Static final int PERSON_DELETE_CODE=1; 5 private Static final int PERSON_UPDATE_CODE=2; 6 private Static final int PERSON_QUERYALL_CODE=3; 7 private static UriMatcher uriMatcher; 8 9 //Person表的数据库帮助对象 10 private PersonSQLiteOpenHelper mOpenHelper; 11 12 static{ 13 uriMatcher =new uriMatcher(UriMatcher.NO_MATH); 14 15 //添加一些uri(分机号) 16 //content://包名+类名/person/insert 17 uriMatcher.addURI(AUTHORITY,"person/insert",PEROSON_INSERT_CODE); 18 uriMatcher.addURI(AUTHORITY,"person/delete",PERSON_DELETE_CODE); 19 uriMatcher.addURI(AUTHORITY,"person/update",PERSON_UPDATE_CODE); 20 uriMatcher.addURI(AUTHORITY,"person/queryall",PERSON_QUERYALL_CODE); 21 } 22 @override 23 public boolean onCreate(){ 24 mOpenHelper=new PersonSQLiteOpenHelper(getContext()); 25 } 26 27 @override 28 public String getType(Uri uri){ 29 30 } 31 32 @override 33 public Uri insert(Uri uri,ContentValues values){ 34 switch(uriMatcher.match(uri)){ 35 case PEROSON_INSERT_CODE: 36 SQLiteDatabase db= mOpenHelper.getWriteableDatabase(); 37 if(db.isopen()){ 38 long id = db.insert("person",null,values); 39 db.close(); 40 return contentUris.withAppendedId(uri,id); 41 } 42 break; 43 44 default: 45 throw new IllegalArgumentException("uri不匹配":+uri); 46 } 47 return null; 48 } 49 50 @override 51 public int delete(Uri uri,String selection,String[] selectionArgs){ 52 //.. 53 } 54 55 @override 56 public int update(Uri uri,ConentValues values,String selection,String[] selectionArgs){ 57 //... 58 } 59 60 @override 61 public Curos query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder){ 62 Switch(uriMatcher.match(uri)){ 63 case PERSON_QUERYALL_CODE: 64 SQLiteDatabase db=mOpenHelper.getReadableDatabase(); 65 if(db.isopen()){ 66 Cusor cor=db.query("person",projection,selection,selectionArgs,null,null,sortOrder); 67 return cor; 68 //db.close();返回cusor结果集时不可以关闭数据库 69 } 70 break; 71 default: 72 throw new IllegalArgumentException("uri不匹配:"+uri); 73 } 74 } 75 76 } 77 78 public class PersonContentResolver{ 79 public void PersonInsert(){ 80 Uri uri=Uri.Parser.("content://包名+类名/person/Insert"); 81 82 //内容提供者对象 83 ContentResolver resolver = getContext().getContentResolver(); 84 ContentValues values=new ContentValues(); 85 value.put("name","小风"); 86 value.put("age","19"); 87 88 uri=resolver.insert(uri,values); 89 long id=ContentUris.parseId(uri); 90 log.i(tag,id); 91 } 92 93 }