2015-4-23
Java构造器
与类名同名;无返回值(void也不行);被不同的修饰符修饰是有区别的;当构造函数被private修饰时,只有本类可访问。其他类可以通过该类的get函数得到对象。如单子模式;子类的构造函数默认调用super(),即父类的构造函数,然后再回到子类自己的构造函数。子类的构造函数中写了super()效果也一样,相当于没写(只能写在第一句,否则出错);构造函数可以重载(当父类有多个构造函数时,可以用super(xxx)来指定调用父类的哪个构造函数);当自定义了构造函数后,系统就不自动生成构造函数了。所以最好为每个类写无参构造函数,以免出错;运行过程:有继承关系时,1>初始化父类的静态块和静态成员变量 2>初始化子类的静态块和静态成员变量 3>执行父类的构造函数 4>执行子类的构造函数;
单子模式,是指一段时间内该类的对象只存在一份。
如
1 //test.java 2 class ace{ 3 private static ace a; 4 private ace(){ 5 System.out.println("1"); 6 } 7 public static ace getAce(){ 8 if( null == a ){ 9 a = new ace(); 10 } 11 return a; 12 } 13 } 14 15 public class test{ 16 public static void main(String[] args) { 17 //ace a = new ace();错 18 ace b = ace.getAce(); 19 ace c = ace.getAce(); 20 21 if( b==c ) 22 System.out.println("equal"); 23 } 24 }
android ListView
1.简单的使用库本身的布局
把数据和布局塞到适配器里,把适配器塞到ListView里。
1 String[] data = {"1","2","3","4","5","6","7","8","9","1","2","3","4","5","6","7","8","9"}; 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, 6 android.R.layout.simple_list_item_1,data); 7 ListView lv = (ListView)findViewById(R.id.listview); 8 lv.setAdapter(adapter); 9 };
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 tools:context="${relativePackage}.${activityClass}" > 6 7 <ListView 8 android:id="@+id/listview" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 ></ListView> 12 </RelativeLayout>
2.自定义每项的布局
自定义了布局diy_list_item、每一行数据的类fruit、继承自ArrayAdapter<fruit>的类fruitAdapter
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 <ImageView 7 android:id="@+id/imageView" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 /> 11 <TextView 12 android:id="@+id/textView" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 /> 16 17 </LinearLayout>
1 package com.example.listviewtest; 2 3 public class fruit { 4 private String name; 5 private int imageId; 6 7 public fruit(String name,int imageId){ 8 this.name = name; 9 this.imageId = imageId; 10 } 11 public String getName(){ 12 return name; 13 } 14 public int getImageId(){ 15 return imageId; 16 } 17 }
1 package com.example.listviewtest; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.ArrayAdapter; 10 import android.widget.ImageView; 11 import android.widget.TextView; 12 13 public class fruitAdapter extends ArrayAdapter<fruit>{ 14 15 private int resourceId; 16 public fruitAdapter(Context context, int resource, List<fruit> objects) { 17 super(context, resource, objects); 18 // TODO Auto-generated constructor stub 19 resourceId = resource; 20 } 21 22 @Override 23 public View getView(int position, View convertView, ViewGroup parent) { 24 // TODO Auto-generated method stub 25 fruit f = getItem(position); 26 View v ; 27 v = LayoutInflater.from(getContext()).inflate(resourceId, null); 28 ImageView fruitImage = (ImageView)v.findViewById(R.id.imageView); 29 TextView fruitName = (TextView)v.findViewById(R.id.textView); 30 fruitImage.setImageResource(f.getImageId()); 31 fruitName.setText(f.getName()); 32 return v; 33 } 34 }
1 package com.example.listviewtest; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.View; 10 import android.widget.AdapterView; 11 import android.widget.AdapterView.OnItemClickListener; 12 import android.widget.ArrayAdapter; 13 import android.widget.ListView; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 18 ArrayList<fruit> fruitList = new ArrayList<fruit>(); 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 initFruit(); 24 fruitAdapter adapter = new fruitAdapter(MainActivity.this, 25 R.layout.diy_list_item,fruitList); 26 ListView lv = (ListView) findViewById(R.id.listview); 27 lv.setAdapter(adapter); 28 } 29 30 private void initFruit(){ 31 fruit one = new fruit("1",R.drawable._1); 32 fruitList.add(one); 33 fruit two = new fruit("2",R.drawable._2); 34 fruitList.add(two); 35 fruit three = new fruit("3",R.drawable._3); 36 fruitList.add(three); 37 fruit four = new fruit("4",R.drawable._4); 38 fruitList.add(four); 39 fruit five = new fruit("5",R.drawable._5); 40 fruitList.add(five); 41 fruit six = new fruit("6",R.drawable._6); 42 fruitList.add(six); 43 fruit seven = new fruit("7",R.drawable._7); 44 fruitList.add(seven); 45 fruit eight = new fruit("8",R.drawable._8); 46 fruitList.add(eight); 47 fruit nine = new fruit("9",R.drawable._9); 48 fruitList.add(nine); 49 } 50 }
问题:
1>diy_list_item.xml里线性布局的宽和高都是match_parent,会不会太大了,留出空白?
2>为什么要写一个fruitAdapter类?(当然这里重写了getView()方法,是可以实现目标的。但不写这个类,用原有的适配器,可以吗?)
Hint: "_1"、"_2"等图片要自己下载,放在res/drawalbe-hdpi下。
3.在public View getView(int position, View convertView, ViewGroup parent)中利用convertView这个缓存来进行优化
1 public View getView(int position, View convertView/*用于将之前加载好的布局进行缓存,以便重用*/, ViewGroup parent) { 2 // TODO Auto-generated method stub 3 fruit f = getItem(position);//该position对应的fruit对象 4 View v ;//理解是diy_list_item这个布局,布局也是一种view 5 ViewHolder vHolder = null;//用于缓存fruitImage和fruitName,避免每次都findViewById() 6 if( null == convertView ){ 7 v = LayoutInflater.from(getContext()).inflate(resourceId, null); 8 vHolder = new ViewHolder(); 9 vHolder.fruitImage = (ImageView)v.findViewById(R.id.imageView); 10 vHolder.fruitName = (TextView)v.findViewById(R.id.textView); 11 v.setTag(vHolder); 12 } 13 else{ 14 v = convertView; 15 vHolder = (ViewHolder) v.getTag(); 16 } 17 vHolder.fruitImage.setImageResource(f.getImageId()); 18 vHolder.fruitName.setText(f.getName()); 19 return v; 20 } 21 //内部类ViewHolder 22 class ViewHolder{ 23 ImageView fruitImage; 24 TextView fruitName; 25 }
4.添加点击事件
1 lv.setOnItemClickListener(new OnItemClickListener() { 2 @Override 3 public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 4 // TODO Auto-generated method stub 5 fruit f = fruitList.get(position); 6 Toast.makeText(MainActivity.this, f.getName(), Toast.LENGTH_SHORT).show(); 7 } 8 });